Play this article
The Object.is()
method determines whether two values are the same or not. It returns true
if the values are the same and false
if not.
let's take a look at the syntax.
Object.is(firstValue, secondValue);
👉 Here are some of the major points you have to remember while using Object.is()
Object.is
doesn't coerce the values as==
do.- It performs
===
on all the compared values except signed zeroes (+0 and -0) andNaNs
. - The
===
operator (and the==
operator) treats the number values -0 and +0 as equal, butObject.is()
consider them unequal. - The
===
operator treatsNumber.NaN
andNaN
as not equal butObject.is
consider them equal. - If objects/arrays are compared then it will compare both values and the reference in memory
- This method is not supported by Internet Explorer yet.
Examples:
// === comparison
Object.is(25, 25); // true
Object.is('foo', 'foo'); // true
Object.is('foo', 'bar'); // false
Object.is(null, null); // true
Object.is(undefined, undefined); // true
Object.is(window, window); // true
Object.is([], []); // false
// Object comparison
const foo = { a: 1 };
const bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
// Case 2: Signed zero
Object.is(0, -0); // false
Object.is(+0, -0); // false
Object.is(-0, -0); // true
Object.is(0n, -0n); // true
// Case 3: NaN
Object.is(NaN, 0/0); // true
Object.is(NaN, Number.NaN) // true
That's it, folks! hope it was a good read for you. Thank you! ✨
👉 References:
The official documentation of Object.is()
👉 Follow me: Github Twitter LinkedIn Youtube
Â