Object.is()
Not a deep-equality helper, despite the name. It compares exactly what === compares, differing on precisely two pairs of values.
Demo
Every case here behaves exactly as === would, which is the honest headline: for ordinary values the two are identical, and === is shorter. The two differences that justify this method cannot be entered through a text box at all. Object.is(NaN, NaN) is TRUE where NaN === NaN is false, and Object.is(0, -0) is FALSE where 0 === -0 is true — but neither NaN nor negative zero survives being typed and parsed as text. Both are shown in the examples below, run against a real runtime.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| a | any | yes | First value. Objects are compared by reference, exactly as with ===. |
| b | any | yes | Second value. |
Return value
boolean — True if the two values are the SameValue. Identical to === except for NaN, which equals itself here, and 0 versus -0, which do not.
Common patterns
if (Object.is(value, NaN)) { }
const isNegZero = Object.is(x, -0);
if (a === b) { }
Examples
Pitfalls
Object.is({a: 1}, {a: 1})
JSON.stringify(a) === JSON.stringify(b)
Object.is(0, -1 * 0)
Object.is(0, (-1 * 0) + 0)
Object.is(1, '1')
Object.is(1, Number('1'))
Object.is(x, NaN)
Number.isNaN(x)
When to use
- A comparison that must treat NaN as equal to NaN
- Distinguishing -0 from 0
- Implementing a general-purpose equality check over values of any type
- Matching the semantics libraries use for change detection
- Ordinary comparison → ===, which is shorter and identical here
- Testing a single value for NaN → Number.isNaN
- Comparing object contents → a deep-equality function
- You want type coercion → == , though generally do not
Notes
FAQ
In exactly two cases, and no others. NaN is equal to itself here and not under ===; 0 and -0 are different here and equal under ===. For every other pair of values the two agree, which is why === remains the right default.
Object.is(NaN, NaN); // true, NaN === NaN is false Object.is(0, -0); // false, 0 === -0 is true