Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.is

Compares two values almost like === but handles NaN and -0 correctly.

Available since: ES2015

Syntax

Object.is(value1, value2)

Parameters

ParameterTypeDescription
value1anyThe first value.
value2anyThe 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 false

For other values the result matches ===:

console.log(Object.is('a', 'a'));
console.log(Object.is({}, {}));

Output:

true
false

Notes

Objects, just as with ===, are compared by reference: two identical-looking objects are not equal.

See also

In the tutorial

  • Comparison — Comparison operators in JavaScript — greater, less, strict and loose equality, comparing strings and NaN.

Thanks for reading!

Donate to the site — we work hard for you.