Object.is vs Strict Equality and the Two Cases They Differ
Object.is and strict equality agree except for two cases, NaN and negative zero. See where negative zero comes from and which comparison each API uses.

Most JavaScript developers use Object.is zero times in a year, and meet it in a surprising error message from a test runner or a framework. It exists to answer one question that === answers badly: are these two values the same value, including the two awkward numbers. The answer differs from === in exactly two places, and the interesting part is where each of those places comes from and which APIs quietly use Object.is for you. This post shows both differences, where negative zero hides, and how the four comparison algorithms in the language relate. Examples run on Node.js 24. The related coverage is in the post on loose equality and the post on NaN.
The two differences fit in one block.
console.log(NaN === NaN, Object.is(NaN, NaN));
console.log(0 === -0, Object.is(0, -0));
console.log("1" === 1, Object.is("1", 1));
console.log({} === {}, Object.is({}, {}));
const shared = { id: 1 };
console.log(shared === shared, Object.is(shared, shared));false true
true false
false false
false false
true trueFor NaN, strict equality says no and Object.is says yes. For zero, strict equality says the two zeros are equal and Object.is says they are not. Everything else agrees: no type coercion, objects equal only to themselves. If you know the values are neither NaN nor zero, the two are interchangeable, and === is the one readers expect.
Where negative zero comes from
IEEE 754 numbers have a sign bit even on zero, and JavaScript exposes both zeros. You rarely type -0, and arithmetic produces it more often than you would guess.
console.log(0 * -1);
console.log(-1 * 0);
console.log(Math.round(-0.4));
console.log(Math.ceil(-0.5));
console.log(Math.sign(-0));
console.log(1 / -0);
console.log(1 / 0);-0
-0
-0
-0
-0
-Infinity
InfinityMultiplying zero by a negative, rounding a small negative number and taking the ceiling of a negative fraction all yield -0. Dividing by it gives -Infinity, which is the one place the sign changes the numeric result. console.log prints -0, but most other ways of showing it hide the sign.
const negative = -0;
console.log(String(negative));
console.log(`${negative}`);
console.log(JSON.stringify(negative));
console.log(negative.toString());
console.log(Object.is(negative, 0));
console.log(negative === 0);0
0
0
0
false
trueConversions to strings drop the sign, so -0 survives only inside a program and vanishes on serialization. That is why bugs involving it are hard to find: the data looks right in logs written through a template string and wrong in a comparison made with Object.is.
Why anyone needs to tell them apart
The sign of zero carries information in a few domains: graphics code where an angle of -0 means "approached from below", physics where it preserves direction through a calculation, and any code that takes 1 / x and cares about the infinity. For most application code it does not matter, and the risk runs the other way. A function that returns -0 and a caller that compares with Object.is gets false for what looks like zero.
function delta(a, b) {
return (a - b) * -1;
}
const change = delta(5, 5);
console.log(change);
console.log(Object.is(change, 0));
console.log(change === 0);
console.log(change + 0);
console.log(Object.is(change + 0, 0));-0
false
true
0
trueThe subtraction gave 0, and multiplying by -1 made it -0. Adding 0 normalizes it, since -0 + 0 is +0, which is the standard trick for removing the sign when you do not want it. If you control the function, normalizing at its boundary prevents the surprise for every caller.
Which APIs use which comparison
The language has four equality algorithms, and different APIs pick different ones. The table is worth keeping.
| Algorithm | NaN with NaN | 0 with -0 | Used by |
|---|---|---|---|
Strict equality (===) | Not equal | Equal | ===, !==, indexOf(), lastIndexOf() |
Loose equality (==) | Not equal | Equal | ==, != |
| SameValueZero | Equal | Equal | includes(), Set, Map keys |
SameValue (Object.is) | Equal | Not equal | Object.is(), property redefinition checks |
SameValueZero is the odd one out: it agrees with Object.is on NaN and with === on zero. It is the comparison collections use, which is why a Set can hold one NaN and treats 0 and -0 as one entry.
console.log(new Set([NaN, NaN, 0, -0]).size);
console.log([NaN].includes(NaN));
console.log([NaN].indexOf(NaN));
console.log([-0].includes(0));
console.log(new Map([[-0, "zero"]]).get(0));2
true
-1
true
zeroThe set holds two items: one NaN and one zero. A Map key of -0 is stored as +0, so looking up 0 finds it.
Frameworks and test tools that use Object.is
React compares state and dependency array entries with Object.is, so a setter called with the same value skips the update, and NaN does not cause an endless loop. The same comparison means a state value of 0 replaced with -0 counts as a change and triggers a render. That is correct and almost never matters.
Node's assert.strictEqual() uses Object.is too, which is easy to forget when a test expecting 0 fails against -0.
import assert from "node:assert/strict";
assert.strictEqual(NaN, NaN);
console.log("NaN passes");
try {
assert.strictEqual(-0, 0);
} catch (error) {
console.log(error.code);
}NaN passes
ERR_ASSERTIONA test failing for -0 against 0 looks absurd in the output, because the two print the same in many reporters. If you meet it, the cause is upstream in the arithmetic, and + 0 or a Math.abs() at the source fixes it.
Writing it by hand
The method can be expressed with strict equality and one extra check, which is a way to understand the two cases.
function sameValue(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
}
return x !== x && y !== y;
}
console.log(sameValue(NaN, NaN));
console.log(sameValue(0, -0));
console.log(sameValue(5, 5));
console.log(sameValue("a", "b"));true
false
true
falseIf the values are strictly equal, they are the same unless both are zero, in which case their reciprocals tell the sign. If they are not strictly equal, the only way to be the same value is for both to be NaN, which the x !== x trick detects. The built-in does this natively, and the code shows how little the two operators actually differ.
What the specification calls them
The language specification names the four algorithms IsStrictlyEqual, IsLooselyEqual, SameValue and SameValueZero, and the names explain the relationships. SameValue is the strictest answer to "are these the same value". SameValueZero relaxes only the zero case, so that +0 and -0 are one key. Strict equality relaxes the NaN case in the other direction, refusing to call it equal to itself. Knowing the names helps when you read an error or a spec note, because API documentation often says "compared using SameValueZero" without explaining it, and the table above is the explanation.
A practical use: a change detector
Code that decides whether a value changed, such as a memoization wrapper, a store subscription or a form dirty check, is the right place for Object.is, because it has to treat an unchanged NaN as unchanged. With === a value that is NaN on both reads reports a change every time, and a subscriber is notified endlessly.
function createStore(initial) {
let value = initial;
const listeners = [];
return {
set(next) {
if (Object.is(value, next)) return;
value = next;
listeners.forEach((listener) => listener(value));
},
subscribe(listener) {
listeners.push(listener);
},
};
}
const store = createStore(NaN);
let calls = 0;
store.subscribe(() => (calls += 1));
store.set(NaN);
store.set(NaN);
store.set(5);
store.set(5);
console.log(calls);1Only the change from NaN to 5 notified the listener. The repeated NaN and repeated 5 were skipped. Replacing Object.is with === makes calls equal to 3, because each NaN write looks like a change. This is the same reasoning React applies to state updates, and it is why a library that stores arbitrary values should use Object.is rather than ===.
Where Object.is is the wrong choice
Use === for ordinary comparisons, since readers expect it and the two differ only in cases you probably do not have. Use Object.is when you are writing a library that must treat NaN as unchanged, or a test helper that must distinguish zeros. Use includes() or a Set for membership.
Do not use it for objects expecting a deep comparison. It compares identity like ===, and two objects with the same contents are different under both. The post on object and array equality covers that case.
The algorithm is defined in the specification as SameValue, and MDN documents the method on its Object.is reference page.


