Skip to the workspace
Pebellslearn by building
Equality Comparisons  ›  Lesson

Object.is and SameValueZero

Object.is 12 min

javascript / Object.is and SameValueZero

Explore each scene at your own pace.

01 · Object.is and SameValueZero

Object.is and SameValueZero

Object.is and SameValueZero

Object.is uses SameValue comparison: it treats NaN as equal to itself and distinguishes +0 from -0. SameValueZero, used by Set and Map keys, treats the two zero signs as equal.

Choose the comparison rule needed for the data instead of treating all equality APIs as interchangeable.

See the mechanism

Walk through the modelObject.is and SameValueZeroAuthored snapshots of the example; use Run below to test the code.
===false
Object.istrue
SameValue explicitly recognizes the two NaN operands as the same value.
1 of 3

Follow one example

javascript

The results are false, true, false and 2. Set combines the two zero signs and the repeated NaN into two distinct entries. None of these operations recursively compares object fields. Two separately created records remain distinct references under these equality rules.

Predict, change and check

Predict: Would Object.is({title: "A"}, {title: "A"}) be true?

Test your explanation: Test separate objects, then compare one object with itself. Separate value comparison rules from the question of object identity.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Match membership behavior to comparison behavior

Object.is exposes SameValue behavior. Set membership, Map keys and includes use SameValueZero, which groups the signs of zero but still recognizes NaN. indexOf cannot find NaN using its comparison behavior. A deduplication feature can therefore differ from a simple indexOf-based implementation.

No comparison in this lesson is a recursive record comparison. If two API responses describe the same article ID, compare the ID field under its defined type contract; do not assume the two response objects share identity. When signed zero matters, include it explicitly in the test inputs.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.