What is Object.is in JavaScript?

The Object.is method is used to check if the two passed arguments are the same. If both arguments are primitive, then the value is checked. If both arguments are objects, then the reference is checked. The Object.is method will return true if both arguments are equal, otherwise, it will return false.

Two values are considered to be equal and return true if

1. Both arguments are undefined

console.log("-----------------------")
console.log("Object.is on undefined")
console.log("Checking - Object.is(undefined,undefined)", Object.is(undefined, undefined) );

2. Both arguments are NaN

console.log("-----------------------")
console.log("Object.is on NaN")
console.log("Checking - Object.is(NaN,NaN)", Object.is(NaN, NaN) );

3. Both arguments are null

console.log("-----------------------")
console.log("Object.is on null")
console.log("Checking - Object.is(null,null)", Object.is(null, null) );

4. Both arguments are the same boolean value

console.log("-----------------------")
console.log("Checking Object.is on boolean")
console.log("Checking - true, true", Object.is(true, true) );
console.log("Checking - false, false", Object.is(false, false) );
console.log("Checking - false,true", Object.is(false, true) );

5. Both arguments are the same String

console.log("-----------------------")
console.log("Checking Object.is on string")
console.log("Checking String - test, test", Object.is("test", "test") );
console.log("Checking String - test, TEST", Object.is("test", "TEST") );

6. Both are objects with the same reference

console.log("-----------------------")
console.log("Checking Object.is on objects")
var obj1 = { a: 1 };
var obj2 = { a: 1 };
console.log(obj1, obj2);
console.log("Checking objects obj1, obj1", Object.is(obj1, obj1) ); // true
console.log("Checking objects obj1, obj2", Object.is(obj1, obj2) );// false

7. Both are numbers with the same value and sign

console.log("Checking Object.is with numbers")
console.log("Checking number -> +0, +0", Object.is(0, 0) );
console.log("Checking number -> -0, -0", Object.is(-0, -0) );
console.log("Checking number -> 100, 100", Object.is(100, 100) );
console.log("Checking number -> +0, -0", Object.is(0, -0) );

How Object.is is different from == and === ?

  • Object.is is not == because, before checking for equality in ==, coercions to both sides are applied. However, in Object.is, these coercions are not applied.
console.log("How == and === is different from Object.is")
console.log("\n---1. '==' applies type coercions to both side whereas Object.is will not do that---")
console.log( " Checking '10' == 10", "10"== 10) // true
console.log( " Checking Object.is('10',10)", Object.is('10', 10)) // false
  • The === and == operators treat the number values -0 and +0 as equal. But in Object.is, it is different.
console.log("---2. +0 and -0 are equal in == and ===, but it is different in Object.is---")
console.log( " Checking +0 == -0 ", +0 == -0) // true
console.log( " Checking +0 === -0 ", +0 === -0) // true
console.log( " Checking +0 -0 with Object.is", Object.is(0,-0) ); // false
  • the === and == operators treat Number.NaN and NaN as not equal. But in Object.is, they are equal.
console.log("\n---3.NaN and Number.NaN are equal in Object.is, but in == and === it is different---");
console.log( " Checking NaN == Number.Nan ", NaN== Number.NaN) // false
console.log( " Checking NaN === Number.Nan ", NaN === Number.NaN) // false
console.log( " Checking NaN, Number.Nan with Object.is", Object.is(NaN, Number.NaN) ); // true

Free Resources