Why you must understand the difference between Reference and Value in JS

Search for a command to run...

No comments yet. Be the first to comment.
Are you listing your apartment for sale? Great! A shiny new listing online, beautiful photos, a detailed description... But you know what? For a buyer, it's just information. For a thief, it's a ready-made plan of attack. It may sound like a scene fr...
How can you satisfy your curiosity in your free time? How can you combine your free time with learning and deepening your technical knowledge? How do you feed that curiosity that keeps growing as you dive deeper into the unknown? There are certainly ...

In the era of the Internet, especially today, when even companies specializing in cybersecurity fall victim to hacker attacks, the question arises: is it possible to win a fight that seems lost from the start? If companies that have been developing s...

In today's world, cyber-attacks are becoming increasingly sophisticated, posing significant challenges in combating cybercriminals. Virtually every imaginable device is interconnected via the Internet, enveloping us in the cyber realm. Cybercriminals...

A fiber optic cable LC type is visible above.

There are two types - primitives and objects or (data types passed by Value and passed by Reference.
Passing by value occurs when assigning primitives.
Value types:
Passing by reference occurs when assigning objects.
Reference types:
When you want to compare objects understanding the difference between values and references is important!
When you use the strict comparison operator "===", two variables having values are equal if they have the same value.
let a = 1
let b = 1
a === b //true
a == b // true
a === 1 //true
When you assign variable b to a JS copies the value, you have two different "1", they are indepentent!
let a = 1
let b = a //copies the same value to variable b
function add(element){
element++
}
add(a) // a = 2
// b = 1
What about reference? The comparison operator "===" works differently when comparing 2 references. References are equal only if they reference exactly the same object.
const person1 = { name: Adrian }
const person2 = { name: Adrian }
const person1Ref = person1
console.log(person1 === person2) // false
console.log(person1 === person1Ref) // true
The comparison operator returns true only when comparing references pointing to the same object: person1 === person1Ref or person1 === person1.
You must understand it when comparing objects. It will make your work easier and avoid mistakes.
You can also compare their structure rather than their reference.