Javascript Object Identities

{} creates a new object.

When you try and compare two, separate new objects (references), they will never be equal.

Laying it out:

var a = {};  // New object, new reference in memory, stored in `a`
var b = {};  // New object, new reference in memory, stored in `b`

a === b;  // Compares (different) references in memory

If it helps, {} is a "shortcut" for new Object(), so more explicitly:

var a = new Object();
var b = new Object();

a === b;  // Still false

Maybe the explicitness of new helps you understand the comparison compares different objects.

On the other side, references can be equal, if they point to the same object. For example:

var a = {};
var b = a;

a === b;  // TRUE

They are different instances of objects, and can be modified independently. Even if they (currently) look alike, they are not the same. Comparing them by their (property) values can be useful sometimes, but in stateful programming languages the object equality is usually their identity.

Tags:

Javascript