Sub topic 13
Strict Equality, Object.is, and SameValueZero
This chapter assumes you already know the seven primitive types, how objects are compared by reference, and the Number edge cases NaN and -0. JavaScript has three equality checks that never convert types. They differ in only two places: how they treat NaN and how they treat -0.
Strict Equality (===)
Strict equality follows a short set of rules:
- If the two operands have different types, the result is
false. No conversion happens. - If both are numbers,
NaNis not equal to anything, itself included, and0equals-0. - If both are strings, they are equal when they contain the same sequence of UTF-16 code units.
- If both are objects, they are equal only when they are the same object.
- Other primitives (booleans, BigInts,
undefined,null, symbols) compare by value. Each symbol equals only itself.
console.log(1 === "1"); // false: different types
console.log(NaN === NaN); // false
console.log(0 === -0); // true
console.log({ x: 1 } === { x: 1 }); // false: different objects
console.log(Symbol("a") === Symbol("a")); // false: each symbol is uniqueObject.is (SameValue)
Object.is uses the SameValue algorithm. It works like === except at the two number edge cases, and there it gives the mathematically "honest" answer.
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is("a", "a")); // trueUse Object.is when you need to tell -0 apart from 0, or when you want a single check that also matches NaN. React uses it to decide whether state has changed.
SameValueZero
SameValueZero has no operator of its own. Built-in collection methods use it. It matches NaN the way Object.is does, and it treats 0 and -0 as equal the way === does.
console.log([NaN].indexOf(NaN)); // -1: indexOf uses ===
console.log([NaN].includes(NaN)); // true: includes uses SameValueZero
console.log(new Set([0, -0]).size); // 1
console.log(new Map([[NaN, "found"]]).get(NaN)); // "found"Comparing the Three
| Comparison | `NaN` vs `NaN` | `0` vs `-0` | Used by |
|---|---|---|---|
| `===` (strict) | false | true | `===`, `!==`, `indexOf`, `switch` |
| SameValue | true | false | `Object.is` |
| SameValueZero | true | true | `includes`, `Set`, `Map` |
For every other pair of values, all three algorithms give the same answer. None of them converts types, so 1 and "1" are unequal under all three. The next chapter covers loose equality (==), which does convert.
Exercise
What do these log: [-0].includes(0), Object.is([-0][0], 0), and [NaN].indexOf(NaN) === [NaN].findIndex(Number.isNaN)?
Show answer
true, because includes uses SameValueZero, which treats -0 and 0 as equal. false, because Object.is tells -0 and 0 apart. false, because indexOf returns -1 (strict equality never matches NaN) while findIndex returns 0.
Exercise
You store values in a Set and add 0, -0, NaN, and NaN. What is the Set's size, and why?
Show answer
2. Set uses SameValueZero, so 0 and -0 count as one value and the two NaN values count as one value.