JS: Object (references) Comparison

When you create an object in Javascript, it stores the object at a unique location in memory.  So even when the objects look identical, they are not the same because JS is comparing to see if they are at the same memory address, not if they look the same or not.

Thus, {} === {}  would return false.

Notes: 

  • Values and variables of same type take same amount of fixed storage (allocated memory).
  • All objects, however, are not of the same size. When a new object is created, a composite memory block consisting of a series of consecutive blocks, one for each instance variable of the object, is created.  The sizes of two objects of different types can be different depending on the number and the types of their instances variables.
  • Since an object variable stores addresses, it also called a pointer variable or reference variable, and the address stored in it a pointer or reference.
Advertisements
JS: Object (references) Comparison