JavaScript Reference ยท Object
Object.is()
Reference for JavaScript Object.is(): compare two values with SameValue semantics, where NaN equals NaN and +0 differs from -0, and how it differs from === .
Object.is() compares two values and returns a boolean. It is almost the same as the strict equality operator ===, with two differences: NaN is considered equal to NaN, and +0 is not equal to -0.
This comparison is called SameValue. It is the rule that tells you whether two values are truly indistinguishable. Most code just uses ===, and reaches for Object.is() in edge cases around NaN and signed zero.
Object.is(NaN, NaN); // true Object.is(0, -0); // false
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.is(value1, value2)
| Name | Description |
|---|---|
value1 |
The first value. |
value2 |
The second value. |
Return value: true if the values are the same by SameValue, otherwise false.
Exceptions: none. No type conversion is performed.
Usage notes
- No coercion. Unlike
==, different types are never equal:Object.is(1, "1")isfalse. - Objects compare by identity. Two separate objects are never the same, even when they hold the same data. Only references to the very same object are equal.
NaNand zeros are the only differences from===. Everything else gives the same answer, soObject.is()is not "stricter" in general.- Checking for
NaN:Object.is(x, NaN),Number.isNaN(x)andx !== xall work.isNaN(x)converts first and is less precise. - Signed zero matters when dividing (
1 / -0is-Infinity) and in some math results. Printing hides it:String(-0)is"0". - Related algorithms:
includes()andSetuse SameValueZero, which treats+0and-0as equal butNaNas equal to itself.indexOf()and===use strict equality, so[NaN].indexOf(NaN)is-1. - Frameworks often use it to decide whether a value really changed (for example in state libraries), so
NaNdoes not trigger endless updates.
Browser support
Object.is() is available in every current browser and JavaScript runtime.
Related
- equality comparison,
isNaN(), comparison operators - Lessons: Comparison, Numbers