inspection

Object.is

Determines whether two values are the same value using the SameValue algorithm. Similar to === but treats NaN as equal to NaN and distinguishes +0 from -0.

Object.is(a, b)

Parameters

Parameter Purpose
a First value to compare
b Second value to compare
returns Boolean — true if the values are the same

Examples

console.log(Object.is(NaN, NaN));

Logs true — unlike NaN === NaN which is false

console.log(Object.is(+0, -0));

Logs false — unlike +0 === -0 which is true

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

Logs true then false — objects compared by reference

console.log(Object.is(1, '1'));

Logs false — no type coercion, same as ===

Gotcha

Differs from === only in two cases: Object.is(NaN, NaN) is true and Object.is(+0, -0) is false. Does not deep-compare objects — reference equality only.

Related methods

← All JS Object methods · Array methods · Spread vs rest