Table Of Contents
Referential Equality
Last Tended:
Status: seed
Primitives
If you compare primitive types; null
, undefined
, string
, number
, boolean
or symbol
using the equality operator ===
, the results is what you would except:
/index.js
Console
Objects
If you compare objects object, array, function
(arrays & functions are a sub-class of object), the result isn't as intuitive:
/index.js
Console
person1
& person2
are both the same type object
.
They have the same properties & values.
It would makes sense if the equality operator returned true
when comparing them, but it doesn't.
This is because when you compare objects, the operator is testing reference equality, not value equality.
Testing whether they are the same instance, not whether they are the same value.
person1
& person2
may have the same value, but they are 2 different object instances.
If I change a property on 1, the other will remain the same.
Thus, when compared using the equality operator, it returns false
.
Value vs. Reference
When assigning or passing variables of a primitive, a copy of the value is created. When assigning or passing variables of an object, a copy of the reference is created.