Skip to content

Instantly share code, notes, and snippets.

@waterswv
Last active November 5, 2019 23:19
Show Gist options
  • Save waterswv/a927ee8f4283a1e9c48acfbef09b20b2 to your computer and use it in GitHub Desktop.
Save waterswv/a927ee8f4283a1e9c48acfbef09b20b2 to your computer and use it in GitHub Desktop.
Referential Equality
// 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