Object.is()

Checks whether two values are the same. It differs from === only for NaN, and for 0 and -0.

Returns
true if the values are the same, otherwise false.
Changes the original
No
On this page

Example

#
JavaScript
console.log(Object.is('Emma', 'Emma'))
console.log(Object.is(NaN, NaN))
console.log(NaN === NaN)
console.log(Object.is(0, -0))
console.log(0 === -0)
Console
true
true
false
false
true

Definition and usage

#

The Object.is(a, b) method returns true when both values are the same value. It works almost like ===: it doesn’t convert types, and it compares objects by reference, so two separate objects with identical contents aren’t equal to each other.

There are only two differences. Object.is(NaN, NaN) returns true, even though NaN === NaN gives false. On the other hand, Object.is(0, -0) returns false, even though 0 === -0 gives true. Negative zero comes up, for example, with -1 * 0 or Math.round(-0.4). The includes() method and the Map and Set collections use an intermediate comparison: for them, NaN equals NaN, and 0 and -0 are equal too.

Day to day, === is enough. Object.is() is useful in code that has to detect every single change in a value, such as libraries that compare state. React uses exactly this algorithm to check whether a new state value differs from the previous one.

Syntax

#
Syntax
Object.is(value1, value2)

Parameters

#
  • value1

    any value

    The first value being compared.
  • value2

    any value

    The second value being compared.

More examples

#
Objects are compared by reference
JavaScript
const settings = { theme: 'dark' }
const sameSettings = settings
const copy = { ...settings }

console.log(Object.is(settings, sameSettings))
console.log(Object.is(settings, copy))
Console
true
false
Where -0 comes from
JavaScript
const values = [Math.round(-0.4), -1 * 0, 0 / -5]

console.log(values)
console.log(values.map((value) => Object.is(value, -0)))
console.log(values.map((value) => value === 0))
Console
[-0, -0, -0]
[true, true, true]
[true, true, true]
Detecting a state change
JavaScript
function hasChanged(previous, next) {
  return !Object.is(previous, next)
}

const state = { points: 10 }
const mutated = state
mutated.points = 20

console.log(hasChanged(state, mutated))
console.log(hasChanged(state, { ...state, points: 30 }))
console.log(hasChanged(NaN, NaN))
Console
false
true
false

A change to a field on the same object isn’t detected, because the reference didn’t change. That’s why state in React is updated with a new copy of the object, not by changing the fields of the old one.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For checking NaN, Number.isNaN(value) reads more clearly. Save Object.is() for situations where you need a general comparison of arbitrary values.
  • Object.is() doesn’t compare the contents of objects. For that you need your own function that compares fields, or a ready-made function from a library.

Related entries

#

Spotted a mistake or missing an example? Write to us.