Ch. 1 · JavaScript

JavaScript Equality: Object Identity and Coercion

JavaScript Equality: Object Identity and Coercion. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Strict equality avoids type coercion, but it does not compare object contents. Two independently created objects are different references. Decide whether the question asks about identity, primitive values or a domain-specific comparison.

Step-by-step walkthrough

Step 1: Define what equal means

Before choosing an operator, identify whether equality means the same object, the same primitive value, or matching domain fields. A user record often needs ID equality even when other fields differ. A configuration comparison may instead care about selected field values.

Step 2: Choose a comparison contract

Use strict equality for ordinary primitive comparisons without coercion. Use Object.is when its NaN and signed-zero behavior matters. For objects, compare a stable domain key or implement a field-based comparison whose treatment of missing values is explicit.

Step 3: Check deceptive examples

Test independently created objects and shared references separately. Include NaN when processing numeric results and distinguish null from missing fields where the application does. An equality helper is only correct relative to the contract it is designed to implement.

Worked scenario

[] === [] is false, while two variables referencing the same array compare equal. NaN === NaN is false; Object.is(NaN, NaN) is true and distinguishes positive and negative zero.

const first = {id: 'u1', name: 'Ada'};
const second = {id: 'u1', name: 'Ada'};
const alias = first;
console.log(first === second); // false
console.log(first === alias); // true
console.log(first.id === second.id); // true
console.log(Object.is(NaN, NaN)); // true
JavaScript

Walk through the example

The first comparison concerns memory identity. The ID comparison concerns domain identity and therefore recognizes the same user across independently parsed responses. Neither statement proves every field is identical. Object.is changes specific primitive edge cases; it still compares objects by reference.

Common mistake

Switching from == to === does not implement deep equality. A deep comparison also needs rules for cycles, dates, missing properties and unordered collections.

Verify the behavior

Test different IDs with the same name, the same ID with updated fields and records missing required IDs. Validate the ID shape before comparison so two malformed records with undefined IDs are not accidentally treated as the same entity.

Interview exercise

Why can two visually identical API records compare unequal?

Answer and reasoning

They were parsed into different objects. Compare a stable identifier or explicitly compare the fields that define equality in the application.

Follow-up discussion

Should we use JSON.stringify for deep equality? Serialization has its own type and property-order rules and does not define domain equality. When is reference equality useful? It can detect retained immutable state or identify a specific in-memory object, provided ownership rules make those references meaningful.

Continue learning

Compare the scenario with the JavaScript interview questions and test your understanding with the JavaScript MCQs. For terminology and implementation details, consult the reference material.

More in JavaScript

read ✓JavaScript · mid

JavaScript Date and Timezone Pitfalls

Parse and format dates without timezone surprises: zero-based months, date-only versus date-time parsing, and why UTC storage avoids drift.

~3 min readread →
read ✓JavaScript · mid

JavaScript Number Precision and BigInt

Why 0.1 + 0.2 is not 0.3, where safe integer range ends, and when BigInt is the right tool for large identifiers and money.

~2 min readread →
esc