JS: Test object equality (reference identity)

By Xah Lee. Date: .

Test object equality (reference identity comparison)

When a object is assigned to a variable, the variable holds a reference to the object.

If 2 objects hold the same reference, triple equal return true.

const x = { "a": 3 };
const y = x;
// x and y holds the same reference
console.assert(x === y);

Triple equal operator on objects with same property values usually return false, because they do not hold the same reference.

// triple equal operator on objects, does not work the way you think

console.assert(([] === []) === false);

console.assert(({} === {}) === false);

console.assert(({ "a": 3 } === { "a": 3 }) === false);

Test equality of array and object by content