Object.is vs Triple Equals in JavaScript
In JavaScript, both Object.is() and the strict equality
operator (===) compare two values to check if they are the
same without performing type conversion. However, they differ in how
they handle two specific edge cases: NaN (Not-a-Number) and
signed zeroes (+0 and -0). While
=== adheres closely to IEEE 754 standard arithmetic rules,
Object.is() implements the “SameValue” algorithm to
determine whether two values are fundamentally identical in memory and
representation.
Key Differences
The differences between Object.is() and ===
come down entirely to two special numerical scenarios.
1. Handling NaN
Under the IEEE 754 standard used by the strict equality operator,
NaN is not equal to any value, including itself. In
contrast, Object.is() considers NaN to be
equal to another NaN.
// Strict equality (===)
console.log(NaN === NaN); // false
// Object.is()
console.log(Object.is(NaN, NaN)); // true2. Handling Signed Zeroes
(+0 vs -0)
The strict equality operator treats positive zero and negative zero
as completely identical. Object.is() differentiates between
the two.
// Strict equality (===)
console.log(+0 === -0); // true
// Object.is()
console.log(Object.is(+0, -0)); // false
console.log(Object.is(+0, 0)); // true
console.log(Object.is(-0, -0)); // trueSimilarities
For all other comparisons, Object.is() and
=== behave identically:
- Primitives: If comparing numbers (other than
NaNand+0/-0), strings, booleans,null,undefined, BigInts, or symbols with the same value, both evaluate totrue. - Objects and Arrays: Both compare references, not
structural contents. Two distinct objects with identical properties will
return
falsefor both methods; they only returntrueif they reference the exact same memory location.
const a = { name: "Alice" };
const b = { name: "Alice" };
const c = a;
// Distinct references
console.log(a === b); // false
console.log(Object.is(a, b)); // false
// Same reference
console.log(a === c); // true
console.log(Object.is(a, c)); // trueSummary Table
| Expression | === Result |
Object.is() Result |
|---|---|---|
NaN === NaN |
false |
true |
+0 === -0 |
true |
false |
'text' === 'text' |
true |
true |
false === false |
true |
true |
{} === {} |
false |
false |
When to Use Each
- Use
===: For standard day-to-day equality checks where conventional arithmetic behavior is preferred (e.g., treating-0and+0the same). - Use
Object.is(): When detecting state changes precisely (such as in React’s rendering engine, which usesObject.isfor hook dependencies) or when you need a reliable way to check if a value is strictlyNaNor a specific signed zero without separate helper functions.