Glossary / Referential equality

Referential equality

Referential equality asks whether two references point to the same object or function. JavaScript’s === and Object.is compare objects by identity, not by their properties. This is distinct from structural or domain equality, where two separately created objects may represent the same user, order, or coordinates.

Identity and change detection

const a = { score: 1 };
const b = { score: 1 };
a === b; // false
const c = a;
c === a; // true

Referential equality enables cheap memoization and shallow change detection. It also creates two common pitfalls: in-place mutation changes contents but preserves identity, so observers may miss a change; reconstructing equivalent objects creates a new identity, so observers may perform unnecessary work. Immutable update patterns copy changed paths and retain references to unchanged subtrees.

There is no universal deep equality. A correct comparison must define how to handle key order, prototypes, cycles, accessors, Maps, Sets, Dates, NaN, signed zero, and domain-specific identity. For entities, compare stable IDs; for numeric geometry, compare with a domain-appropriate tolerance.

Interview questions

  1. Why are two identical object literals unequal? They are different object identities.
  2. How can in-place mutation break memoization? The reference stays the same, so shallow identity checks see no change.
  3. Why can new equivalent objects cause re-rendering? Their references differ.
  4. How compare entities robustly? Use stable domain keys or an explicit comparator.

References