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
Object.assign
Copies all enumerable own properties from one or more source objects to a target object, then returns the modified target. Performs a shallow merge, invoking source getters and target setters.
Object.freeze
Freezes an object: new properties cannot be added, existing properties cannot be removed, and their values, writability, and configurability cannot be changed. Returns the same object.
Object.keys
Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.
Object.values
Returns an array of a given object's own enumerable string-keyed property values. Introduced in ES2017 as a companion to Object.keys.
Object.entries
Returns an array of a given object's own enumerable string-keyed [key, value] pairs. Useful for iterating objects with for...of destructuring or converting to a Map.