Sub topic 14
Strict Equality, Object.is, and SameValueZero
This chapter assumes you know the seven primitive types, that objects are compared by reference, and the Number edge cases covered earlier: NaN, -0, and floating-point precision. You will also need a browser console or Node.js to run the examples.
JavaScript has four equality algorithms. Loose equality (==) converts types and has its own chapter next. The other three never convert types. They are strict equality (===), SameValue (exposed as Object.is), and SameValueZero (used inside built-ins such as includes, Set, and Map). These three agree on almost every input. They differ only on NaN and on 0 versus -0. Those two cases cause real bugs, so this chapter treats them in detail.
How Strict Equality Works
The specification calls === IsStrictlyEqual. It runs these steps:
- If the two operands have different types, return
false. No conversion happens. - If both are Numbers: return
falseif either isNaN. Treat+0and-0as equal. Otherwise compare the numeric values. - If both are BigInts, compare their mathematical values.
- For every other type, compare by "same value without numbers":
- Strings are equal when they contain the same sequence of UTF-16 code units.
- Booleans,
undefined, andnullare equal to themselves. - Symbols are equal only to the same symbol.
- Objects are equal only when both operands refer to the same object.
console.log(1 === 1); // true
console.log(1 === "1"); // false: types differ
console.log(1n === 1); // false: bigint vs number
console.log(null === undefined); // false: types differ
console.log(NaN === NaN); // false
console.log(0 === -0); // true
console.log(Symbol("a") === Symbol("a")); // false: two distinct symbols
const box = { n: 1 };
console.log(box === box); // true: same object
console.log(box === { n: 1 }); // false: different objectsGotcha: Strings That Look Identical
Strict equality compares code units, not how the text looks on screen. Unicode can encode "é" as one code point (U+00E9) or as "e" plus a combining accent (U+0301). The two render the same but are not ===.
const composed = "\u00e9";
const decomposed = "e\u0301";
console.log(composed === decomposed); // false
console.log(composed.length, decomposed.length); // 1 2
console.log(composed.normalize() === decomposed.normalize()); // trueCall normalize() on user-entered text before you compare it or use it as a lookup key.
Object.is: The SameValue Algorithm
Object.is(x, y) follows the same steps as === with two exceptions in the Number step:
NaNequalsNaN.+0and-0are different.
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is(-0, -0)); // true
console.log(Object.is("a", "a")); // true
console.log(Object.is({}, {})); // false: still identity for objectsObject.is is not "deep equality". It compares objects by identity, exactly like ===. Its purpose is to tell you whether two values are indistinguishable. NaN and NaN behave the same everywhere. 0 and -0 do not: 1 / 0 is Infinity and 1 / -0 is -Infinity.
Object.defineProperty uses SameValue when it decides whether a change to a non-writable property is allowed. Libraries use it for change detection. React, for example, compares state and hook dependencies with Object.is, so setting state to NaN twice does not trigger a second render.
SameValueZero: The Built-in Collections' Choice
SameValueZero sits between the other two. It treats NaN as equal to NaN, like Object.is, and treats +0 as equal to -0, like ===. It has no operator of its own. You meet it through these built-ins:
Array.prototype.includesandTypedArray.prototype.includesSetmembershipMapkeys
const values = [1, NaN, 0];
console.log(values.indexOf(NaN)); // -1: indexOf uses ===
console.log(values.includes(NaN)); // true: includes uses SameValueZero
console.log(values.includes(-0)); // true: -0 matches 0
const seen = new Set([NaN, NaN, 0, -0]);
console.log(seen.size); // 2: one NaN, one zero
const labels = new Map([[-0, "zero"]]);
console.log(labels.get(0)); // "zero"
console.log(Object.is([...labels.keys()][0], -0)); // false: stored as +0The last line shows a detail people often miss. Set and Map convert a -0 key to +0 when it is stored. If you put -0 into a Set and read it back, you get +0.
Gotcha: Not Every Search Method Agrees
| Method or construct | Algorithm | Finds `NaN`? | `-0` matches `0`? |
|---|---|---|---|
| `===`, `!==` | IsStrictlyEqual | No | Yes |
| `switch` cases | IsStrictlyEqual | No | Yes |
| `indexOf`, `lastIndexOf` | IsStrictlyEqual | No | Yes |
| `Object.is` | SameValue | Yes | No |
| `includes` | SameValueZero | Yes | Yes |
| `Set`, `Map` | SameValueZero | Yes | Yes |
The switch row matters in practice. A case NaN: label can never match, because switch compares with ===.
function describe(x) {
switch (x) {
case NaN:
return "not a number"; // unreachable
default:
return "something else";
}
}
console.log(describe(NaN)); // "something else"To find NaN by position, use findIndex with an explicit test: values.findIndex(Number.isNaN).
Choosing an Algorithm
Use === by default. It is fast, it never converts types, and it gives the answer most code expects. 0 === -0 being true is almost always what you want for money, counts, and indexes.
Use Number.isNaN(x) when you need to detect NaN. It states your intent more clearly than Object.is(x, NaN).
Use Object.is when -0 carries meaning. Examples include direction or sign in physics and graphics code, or a test helper that must report the exact value it received. Test frameworks such as Jest use Object.is for toBe, so expect(-0).toBe(0) fails.
Use includes, Set, or Map when you want membership checks that handle NaN. Remember that they treat -0 and 0 as one value and keep only +0.
Writing Your Own SameValueZero
The rules are short enough to implement yourself. Doing so is a good way to confirm you understand them.
function sameValueZero(x, y) {
if (typeof x === "number" && typeof y === "number") {
// NaN is the only value not equal to itself
return x === y || (x !== x && y !== y);
}
return x === y;
}
console.log(sameValueZero(NaN, NaN)); // true
console.log(sameValueZero(0, -0)); // true
console.log(sameValueZero(1, "1")); // falseA matching sameValue adds one more check. When x === y and both are 0, compare 1 / x === 1 / y to tell +0 from -0.
Exercise
Predict each result: [NaN].indexOf(NaN), [NaN].includes(NaN), Object.is(-0, 0), new Set([-0, 0, NaN]).size, and 1n === 1.
Show answer
-1, true, false, 2, false. indexOf uses ===, so NaN is never found. includes uses SameValueZero, which matches NaN. Object.is separates -0 from 0. The Set holds one zero and one NaN. 1n === 1 is false because a bigint and a number are different types and === never converts.
Exercise
A function receives a value and must return true only if the value is exactly -0. Write it without using Object.is.
Show answer
function isNegativeZero(x) { return x === 0 && 1 / x === -Infinity; } The first check accepts both zeros and rejects every other value, including "0", because === does not convert. Dividing 1 by -0 gives -Infinity, and dividing 1 by +0 gives Infinity, which separates the two zeros.
Exercise
A user types "café" into a search box, and your list contains "café" stored with a combining accent. list.includes(input) returns false. Why, and how do you fix it?
Show answer
includes compares strings by UTF-16 code units. The precomposed "é" (U+00E9) and "e" plus U+0301 are different sequences. Normalize both sides to the same form before comparing, for example list.map(s => s.normalize()).includes(input.normalize()).