Object.is
Compares two values almost like === but handles NaN and -0 correctly.
Available since: ES2015
Syntax
Object.is(value1, value2)Parameters
| Parameter | Type | Description |
|---|---|---|
| value1 | any | The first value. |
| value2 | any | The second value. |
Return value
booleantrue if the values are the same.
Examples
Differences from strict equality:
console.log(NaN === NaN, Object.is(NaN, NaN));
console.log(0 === -0, Object.is(0, -0));Output:
false true
true falseFor other values the result matches ===:
console.log(Object.is('a', 'a'));
console.log(Object.is({}, {}));Output:
true
falseNotes
Objects, just as with ===, are compared by reference: two identical-looking objects are not equal.
See also
- Strict equality (===) — Compares values without type conversion.
- Number.isNaN — Checks that a value is NaN, without type conversion.
In the tutorial
- Comparison — Comparison operators in JavaScript — greater, less, strict and loose equality, comparing strings and NaN.