Sub topic 9
Object.is and the SameValue Family
This chapter assumes you already know how === works and that you can run snippets in Node.js or a browser console. The previous chapters covered strict and loose equality. JavaScript actually defines three "same value" algorithms, and different built-in features use different ones.
Three Algorithms, Three Answers
The ECMAScript specification names these algorithms:
- IsStrictlyEqual: used by
===,!==,indexOf,lastIndexOf, andswitchcases. - SameValue: used by
Object.is. - SameValueZero: used by
includes,Mapkeys, andSetmembership.
All three agree on almost every pair of values. They differ only for NaN and for the two zeros, 0 and -0.
| Comparison | `===` | `Object.is` (SameValue) | SameValueZero |
|---|---|---|---|
| `NaN` vs `NaN` | false | true | true |
| `0` vs `-0` | true | false | true |
| `'a'` vs `'a'` | true | true | true |
| `{}` vs `{}` | false | false | false |
A simple way to remember it: SameValueZero is SameValue, except that it treats the two zeros as equal.
Seeing It in Code
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is('hi', 'hi')); // true
const list = [NaN, -0];
console.log(list.indexOf(NaN)); // -1 (strict equality)
console.log(list.includes(NaN)); // true (SameValueZero)
console.log(list.includes(0)); // true (SameValueZero ignores the zero sign)
const set = new Set([NaN, NaN, 0, -0]);
console.log(set.size); // 2: one NaN, one zero
const map = new Map();
map.set(-0, 'zero');
console.log(map.get(0)); // 'zero'
console.log(Object.is([...map.keys()][0], -0)); // false: Map stores the key as +0The last line shows a detail people often miss. Map and Set convert a -0 key to +0 when they store it, so you cannot get the sign back later.
When to Reach for Object.is
Use === by default. It is fast, familiar, and correct for nearly all application code. Choose Object.is when one of the edge cases matters:
- Change detection. React and similar libraries compare old and new state with
Object.is. A state value that staysNaNtherefore does not trigger a re-render on every update. - Numeric code where the sign matters.
1 / -0is-Infinity, so a math library may need to tell the zeros apart. - Tests and assertions. Test helpers such as Jest's
toBeuseObject.is, soexpect(NaN).toBe(NaN)passes.
Exercise
Predict the output: console.log([0].includes(-0), Object.is([0].indexOf(-0), 0), new Set([-0]).has(0));
Show answer
true true true. includes uses SameValueZero, so -0 matches 0. indexOf uses strict equality, where 0 === -0, so it returns index 0, and Object.is(0, 0) is true. Set uses SameValueZero, so has(0) finds the stored zero.
Exercise
You write a function isSame(a, b) that should behave like Object.is but may only use ===. Which two cases do you need to handle specially?
Show answer
NaN and signed zero. If a !== a, then a is NaN, so return b !== b. If a === b and a === 0, compare 1 / a === 1 / b to tell 0 from -0. In every other case return a === b.