Last active
November 5, 2019 23:19
-
-
Save waterswv/a927ee8f4283a1e9c48acfbef09b20b2 to your computer and use it in GitHub Desktop.
Referential Equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here's a test on referential equality in Javascript. | |
1. {} === {} // false | |
const a = {} | |
2. a === a // true | |
/* So why does 1 evaluate to false but 2 evaluates to true? | |
The answer has to do with how Javascript creates and stores items in memory. | |
In #1 you are comparing an object with an object ... however, you have created 2 seperate Objects at once | |
These 2 objects are composed of the same details but are distinctly different and occupy different spots in memory. | |
In # 2 you are creating both variable 'a', a new Object {}, and storing a reference to that new Object in variable 'a'. | |
Thus when you compare a with a ... it evaluates to true becuase it is the same object. | |
Whereas #1 is false because it is evaluating 2 separate objects. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment