Why === Isn’t Enough: Mastering Object.is() for Precise Equality in JavaScript
While the strict equality operator (===) works well in most cases, it fails with special values like NaN and signed zero; JavaScript’s Object.is() method addresses these edge cases by providing true NaN comparison and distinguishing +0 from -0, offering a more precise equality check.
“Don’t use ===, don’t use ==!” However, even the seemingly perfect === can expose problems in extreme scenarios.
Now it’s time to learn about a new member that doesn’t aim to replace === but to fill its gaps, making the concept of “equality” in JavaScript more precise and intuitive.
Strict Equality ===
===rule: both value and type must be identical; no type conversion occurs.
'1' === 1; // false, different types (string vs number)
true === 1; // false, different types (boolean vs number)
null === undefined; // false, different typesIn 99% of scenarios, === works great—clear, reliable, expected. Yet it struggles with two “special citizens” of JavaScript.
=== ’s Two Problems
1. NaN’s self‑denial NaN represents a “not a number” result per IEEE‑754 and is not equal to any value, including itself. NaN === NaN; // false This is mathematically correct but inconvenient in code; methods like indexOf or includes (pre‑ES6) fail to find NaN in arrays.
2. Flattened positive and negative zero
JavaScript’s number 0 has two representations: +0 (or 0) and -0. They differ in some low‑level contexts, but === treats them as equal. -0 === +0; // true For most applications this is fine, but high‑precision math may suffer.
More Precise Same‑Value Equality
To solve these two issues, ES introduced a new API: Object.is(), defined as “same‑value equality”.
The behavior of Object.is() can be summarized as:
In most cases, Object.is(a, b) yields the same result as a === b . The only differences are how it handles NaN and -0 .
Let’s see how Object.is() perfectly resolves the problems:
1. NaN equals NaN Object.is() considers NaN and NaN equal. Object.is(NaN, NaN); // true This makes checking for NaN more intuitive (though Number.isNaN() remains a specialized tool).
2. Distinguishing +0 and -0 Object.is() can clearly differentiate positive zero from negative zero.
Object.is(-0, +0); // false
Object.is(0, -0); // false
Object.is(0, 0); // trueMany newer JavaScript features already rely on Object.is() ’s same‑value logic, such as key matching in Map and value storage in Set. Object.is() is not a replacement for === but a powerful complement, providing a stricter definition of equality in JavaScript.
In practice, use === by default for its speed and reliability, and switch to Object.is() when dealing with special cases involving NaN or -0.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
