Advanced JavaScript Interview QuestionsAdvancedComparison
JavaScript · Question 74
What is the difference between ===, Object.is(), and SameValueZero equality in JavaScript?
Direct answer
Strict equality treats +0 and -0 as equal and NaN as unequal to itself; Object.is() distinguishes signed zero and treats NaN as equal to itself; SameValueZero treats signed zero as equal and NaN as equal.
These algorithms differ only in a few edge cases, but those edge cases explain real API behavior. NaN === NaN is false, while Object.is(NaN, NaN) is true. Conversely, 0 === -0 is true, while Object.is(0, -0) is false.
SameValueZero combines the usually convenient choices: NaN compares equal to itself and signed zero compares equal. Collections and lookup operations such as Set, Map key matching, and Array.prototype.includes() use SameValueZero-style comparison.
- Use
===for ordinary strict comparisons. - Use
Object.is()whenNaNand signed zero distinctions matter. - Do not assume all collection membership checks use
===; APIs can specify a different equality algorithm.