AceDevHub
Intermediate JavaScript Interview QuestionsIntermediateConcept

JavaScript · Question 59

Why does === not perform deep equality for objects, and what makes deep equality difficult?

Direct answer

For objects, strict equality compares identity: two references are equal only when they refer to the same object; deep equality requires an explicit policy for recursively comparing structure and special value types.

{} === {} is false because each object literal creates a distinct object. In contrast, const a={}; const b=a; a===b is true because both variables contain the same object reference.

  • A deep comparison must decide whether property order matters and whether inherited or only own properties count.
  • It needs rules for arrays, Date, RegExp, Map, Set, typed arrays, and other built-ins.
  • Cyclic graphs require tracking already-compared object pairs to avoid infinite recursion.
  • Functions and custom class instances raise semantic questions: identity, source text, prototype, or domain-specific equality?

Object.is() is another identity/value comparison primitive with notable differences from === for NaN and signed zero; it is still not deep equality.