Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Equality Comparisons  ›  Capstone Project

Object.is and SameValueZero

Object.is 12 minObject.is works like strict equality except that NaN equals NaN and 0 differs from -0, while SameValueZero, used by includes, Map and Set, treats both NaN and the zeros as equal.

Object.is and SameValueZero

In short: Object.is(a, b) behaves exactly like ===, except it fixes two number quirks: Object.is(NaN, NaN) is true (where === says false), and Object.is(0, -0) is false (where === says true). Everything else matches === exactly. A close cousin called SameValueZero — used internally by includes, Map and Set — takes the NaN fix from Object.is but keeps the 0/-0 behaviour of ===. For everyday code, default to ===; reach for Object.is only when NaN or signed zero must be handled exactly.

A table of four comparisons against NaN vs NaN and 0 vs -0: == gives false/true, === gives false/true, SameValueZero (includes/Map/Set) gives true/true, Object.is (SameValue) gives true/false — only NaN and -0 tell the four apart; everywhere else they agree

Fix a false "value changed" alert

=== says two NaNs are different, which breaks a naive "did this value change?" check when the value legitimately is NaN both times (a missing sensor reading, say):

javascript

old !== next is true — the screen would redraw for nothing, forever, because NaN never equals itself under ===. !Object.is(old, next) is false: Object.is treats the two NaNs as the same value, so nothing looks changed.

Did the reading change?Step 1 of 4
  1. The values

    Old reading: NaN. New reading: NaN.

Compare Object.is against === directly

Running the same pairs through both operators side by side shows exactly where they part ways — and nowhere else:

javascript

Same-type primitives ("Blue Hour") and mismatched objects ({} vs {}) behave exactly like ===. Only the two number rows in the drawing's table differ.

Find NaN in an array two different ways

includes and indexOf look like they should agree, but they use different equality rules internally — includes uses SameValueZero, indexOf uses ===:

javascript

includes finds it: true. indexOf never can, because it compares with ===, and NaN === NaN is always false, so it reports -1 — "not found" — even though NaN is sitting right there at index 0.

Quick practice

quick drill

What is Object.is(NaN, NaN)?

quick drill

What is Object.is(0, -0)?

quick drill

What is Object.is("5", 5)?

quick drill

What does [NaN].indexOf(NaN) return?

quick drill

Which rule says NaN equals NaN AND 0 equals -0?

🎯 Your turn

For the pairs (NaN, NaN) and (0, -0), explain what ===, Object.is and includes (SameValueZero) each say, and when you would choose Object.is in real code.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFour ways to compare

For the pairs (NaN, NaN) and (0, -0), explain what ===, Object.is and includes (SameValueZero) each say, and when you would choose Object.is in real code.