Reference and Value: Why you should know the difference
In the following examples emojis are used to name variables and should not be replicated, otherwise your code will return a syntax error.
If for some reason you've tried comparing objects or arrays in JavaScript, you probably went confused by why they weren't the same even having an identical value/content..
This happens because JavaScript compares non-primitive types with their reference and not their actual value, and not knowing that, can cause a headache.
Copy by Value
Primitive data types like String, Number, Boolean, undefined, null, are tagged as Value types, because, when we throw its information or value in some other variable, this new variable will copy its value.
See the following example.
_8let π = "Apple"_8let π° = 0_8_8let π = π // "Apple"_8let π = π° // 0_8_8console.log(π === π) // true_8console.log(π === π°) // true
When comparing the new variables using console.log(), it will return true, because JavaScript compares the primitives according to their content.
Copy by Reference
On the other hand, the non-primitives, Arrays, Objects, and Functions, are tagged as Reference types. Because, when we define a new variable with its information, this new variable will have its reference and not its value.
See the following example
_6const π = [πΊ, πΊ, πΊ, πΊ]_6const π = [πΊ, πΊ, πΊ, πΊ]_6const π = π_6_6console.log(π === π) // false_6console.log(π === π) // true
Let's go back to some lines in the text "this new variable will have its reference and not its value". But, if it won't have the value, how did javascript manage to compare the π with π? And why does π when compared to π, JavaScript tell us that it's "different"?
The answer is simple, comparison by reference! Primitive data types are copied and compared by value, and non-primitive ones are copied and compared by reference.
So knowing the difference between reference and actual value is critical for learning advanced hooks in React like useEffect(), useCallback() and useMemo().
Ok, but what is Reference?
References are addresses allocated in your computer's memory, and when we compare two non-primitive pieces of information, for example, an object, JavaScript compares its memory address and not its contents. Let's see one more example.
_13const π©ββοΈ = {_13 name: "Bia",_13 age: 28_13};_13_13const π©βπ³ = π©ββοΈ;_13_13console.log(π©ββοΈ.name); // Bia_13_13π©βπ³.nome = "Patricia";_13_13console.log(π©ββοΈ.name); // Patricia_13console.log(π©βπ³.name); // Patricia
When we set the name of π©βπ³ to Patricia, the name of π©ββοΈ also changes! That's because the content of π©βπ³ is a reference to the content of π©ββοΈ, so whatever changes there are in any reference to the Object, Array, Function the original one will be affected.
Why knowing this is important?
If you've been using React for a while, we have several hooks and features that help our program's performance, and an important one is the useEffect()'s dependency array. If you put an array or object as a dependency, your page will not work correctly, and that's because useEffect() is comparing non-primitives with their reference.
Another example is the useCallback(), used to memorize functions and not just compare with their reference, preventing the creation of another identical function on every rendering. If you want a post talking about it, let me know!
Remember!
β’ Value types are Primitives and copied by their value.
β’ Reference types are non-primitives and are copied by their reference.
β’ When comparing identical non-primitives, they return false because their reference type.
β’ Value types β String, Number, Boolean, undefined and null.
β’ Reference types β Array, Object and Function.