Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
Object.is(NaN, NaN); // true
Object.is(0, -0);    // false

Try it

JS Where it differs from ===

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Detecting negative zero

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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") is false.
  • 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.
  • NaN and zeros are the only differences from ===. Everything else gives the same answer, so Object.is() is not "stricter" in general.
  • Checking for NaN: Object.is(x, NaN), Number.isNaN(x) and x !== x all work. isNaN(x) converts first and is less precise.
  • Signed zero matters when dividing (1 / -0 is -Infinity) and in some math results. Printing hides it: String(-0) is "0".
  • Related algorithms: includes() and Set use SameValueZero, which treats +0 and -0 as equal but NaN as 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 NaN does not trigger endless updates.

Browser support

Object.is() is available in every current browser and JavaScript runtime.