I stumbled upon a code example which was using this comparison:

var someVar = 0;
Object.is(false, someVar); //Returns false 

I know false == 0 will be true that's why we have ===.

How is Object.is different from ===?


Solution 1:

=== is called strict comparison operator in JavaScript. Object.is and strict comparison operator behave exactly the same except for NaN and +0/-0.

From MDN:

Object.is() method is not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.

Code below highlights the difference between === and Object.is().

console.log(+0 === -0); //true
console.log(Object.is(+0, -0)); //false

console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); //true

console.log(Number.NaN === Number.NaN); // false
console.log(Object.is(Number.NaN, Number.NaN)); // true

console.log(NaN === Number.NaN); // false
console.log(Object.is(NaN, Number.NaN)); // true

enter image description here

You can find more examples here.

Note: Object.is is part of the ECMAScript 6 proposal and is not widely supported yet (e.g. it's not supported by any version of Internet Explorer or many older versions of other browsers). However you can use a polyfill for non-ES6 browsers which can be found in link given above.

Solution 2:

Object.is uses the specification's SameValue algorithm, whereas === uses the Strict Equality Algorithm. A note on the Strict Equality Algorithm calls out the difference:

This algorithm differs from the SameValue Algorithm...in its treatment of signed zeroes and NaNs.

Note that:

  • NaN === NaN is false, but Object.is(NaN, NaN) is true
  • +0 === -0 is true, but Object.is(+0, -0) is false
  • -0 === +0 is true, but Object.is(-0, +0) is false

JavaScript has at least four kinds of "equality":

  • "Loose" (==), where the operands will be coerced to try to make them match. The rules are clearly specified, but non-obvious. ("" == 0 is true; "true" == true is false, ...).
  • "Strict" (===), where operands of differing types will not be coerced (and will not be equal), but see note above about NaN and positive and negative zero.
  • SameValue - as listed above (used by Object.is).
  • SameValueZero - like SameValue except +0 and -0 are the same instead of different (used by Map for keys, and by Array.prototype.includes).

There's also object equivalence, which isn't provided by the language or runtime itself, but is usually expressed as: The objects have the same prototype, same properties, and their property values are the same (by some reasonable definition of "the same").


SameValue algorithm:

  • If Type(x) is different from Type(y), return false.
  • If Type(x) is Number, then
    • If x is NaN and y is NaN, return true.
    • If x is +0 and y is -0, return false.
    • If x is -0 and y is +0, return false.
    • If x is the same Number value as y, return true.
    • Return false.
  • Return SameValueNonNumber(x, y).

...where SameValueNonNumber is:

  • Assert: Type(x) is not Number.
  • Assert: Type(x) is the same as Type(y).
  • If Type(x) is Undefined, return true.
  • If Type(x) is Null, return true.
  • If Type(x) is String, then
    • If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
  • If Type(x) is Boolean, then
    • If x and y are both true or both false, return true; otherwise, return false.
  • If Type(x) is Symbol, then
    • If x and y are both the same Symbol value, return true; otherwise, return false.
  • Return true if x and y are the same Object value. Otherwise, return false.

Strict Equality Algorithm:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.
    • If x is the same Number value as y, return true.
    • If x is +0 and y is -0, return true.
    • If x is -0 and y is +0, return true.
    • Return false.
  3. Return SameValueNonNumber(x, y).