Typorithm · medium

Reference Equality

reference-equalityinterview-questionsreferencesvalue-equalityobject-is
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Reference Equality

0%

Copy & Equality

Objects and arrays compare by reference: {} === {} and [1] === [1] are both false because each is a distinct object. Primitives compare by value, so 2 === 2 is true. Object.is(NaN, NaN) is true, where === would be false.

=== on objects and arrays compares identity, not contents. {} === {} is false and [1] === [1] is false because each literal creates a brand-new object with its own reference.

Two variables are === only when they point at the *same* object. Primitives are different: numbers, strings, and booleans compare by their value, so 2 === 2 is true.

Object.is is a stricter sibling of ===. It treats NaN as equal to itself — Object.is(NaN, NaN) is true while NaN === NaN is false — and distinguishes +0 from -0.

01

{} === {} and [1] === [1] are false — distinct references.

02

Primitives compare by value, so 2 === 2 is true.

03

Object.is(NaN, NaN) is true; NaN === NaN is false.

console.log({} === {})        // false — different references
console.log([1] === [1])      // false — different references
console.log(2 === 2)          // true — primitives compare by value
console.log(Object.is(NaN, NaN)) // true

Read the implementation

Connect each part of the code to the concept before you type.

1. Two object literals

Each {} is a distinct object, so === compares their references and returns false.

console.log({} === {})        // false — different references

2. Two arrays

Arrays are objects too; two separate [1] arrays are different references.

console.log([1] === [1])      // false — different references

3. Primitives by value

Primitives like numbers compare by their value, so 2 === 2 is true.

console.log(2 === 2)          // true — primitives compare by value

4. Object.is and NaN

Object.is(NaN, NaN) is true, unlike NaN === NaN which is false.

console.log(Object.is(NaN, NaN)) // true

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Reference Equality: Lesson & Practice · Typorithm | ApplyDjinn