JavaScript Interview Question #147

What is the difference between deep equality and reference equality for objects?

Generators, Browser Storage, Security & Advanced Runtime Concepts Senior Advanced

Quick Interview Answer

For objects, strict equality compares object identity rather than recursively comparing property values. Two separately created objects with identical properties are still different references and therefore are not strictly equal.

Detailed Explanation

For objects, strict equality compares object identity rather than recursively comparing property values. Two separately created objects with identical properties are still different references and therefore are not strictly equal.

Deep equality requires application-specific comparison logic or a suitable library because requirements vary for nested objects, arrays, dates, maps, sets, prototypes, cycles, and property ordering.

Code Example

const a = { id: 1, name: "Asif" };
const b = { id: 1, name: "Asif" };
const c = a;

console.log(a === b); // false
console.log(a === c); // true

// Deep equality requires comparing the relevant structure
// according to the application's rules.