Object. is()
Checks whether two values are the same. It differs from === only for NaN, and for 0 and -0.
- Returns
trueif the values are the same, otherwisefalse.- Changes the original
- No
Example
#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)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
#Object.is(value1, value2)Parameters
#value1
any value
The first value being compared.value2
any value
The second value being compared.
More examples
#const settings = { theme: 'dark' }
const sameSettings = settings
const copy = { ...settings }
console.log(Object.is(settings, sameSettings))
console.log(Object.is(settings, copy))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))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))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. SaveObject.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
#- ===The strict equality operator compares values without type conversion: true only for the same type and the same value.
- Number.isNaN()Checks whether a value is exactly NaN.
- Array.prototype.includes()Checks whether an array contains the given value.
- SetA collection of unique values: each value can appear in the set only once.
- Spread (...)The spread syntax spreads an array, string, or object out into individual elements: for copying, merging, and passing arguments.
Spotted a mistake or missing an example? Write to us.