Object.is vs ===

JavascriptEcmascript 6

Javascript Problem Overview


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 ===?

Javascript Solutions


Solution 1 - Javascript

=== 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 - Javascript

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).

Solution 3 - Javascript

Summary:

The Object.is() function takes 2 values as arguments and returns true if the 2 given values are exact the same, otherwise it will return false.

Why do we need this?

You might think, we already have strict equality (checks type + value) checking in javascript with the === operator, why do we need this function? Well strict equality isn't sufficient in some cases and they are the following:

console.log(NaN === NaN);   // false
console.log(-0 === +0);     // true

Object.is() helps us by being able to compare these values to see if they are similar, something the strict equality operator cannot do.

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

Solution 4 - Javascript

Object.is = function(v1, v2){
  //test for `-0`
  if(v1 === 0 && v2 === 0) {
    return 1 / v1 === 1 / v2;
  }
  
  //test for `NaN`
  if(v1 !== v1) {
    return v2 !== v2;
  }
  
  //everything else
  return v1 === v2;
}

The above is the polyfill function to show how Object.is works, for anyone who are interested to know. A reference to You-Don't-Know-JS

Solution 5 - Javascript

In a nutshell, they are similar, but Object.is is smarter and more accurate...

Let's look at this...

+0 === -0 //true

But this is not fully right as it ignores - and + before...

Now we use:

Object.is(+0, -0) //false

As you see, this is more accurate to compare.

Also in case of NaN that works more like correct, as consider any NaN the same.

Solution 6 - Javascript

=== is considered a strict comparison and will return a boolean indicating whether or not the operands are of the same type and the contents match.

Object.is behaves the same as === expect for the +/- 0 and NaN cases

console.log({} === {}) // false
console.log(Object.is({}, {})) // false

console.log(1 === 1) // true
console.log(Object.is(1, 1)) // true

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJS-JMS-WEBView Question on Stackoverflow
Solution 1 - JavascriptGurpreet SinghView Answer on Stackoverflow
Solution 2 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 3 - JavascriptWillem van der VeenView Answer on Stackoverflow
Solution 4 - JavascriptIsaacView Answer on Stackoverflow
Solution 5 - JavascriptAlirezaView Answer on Stackoverflow
Solution 6 - JavascriptRan TurnerView Answer on Stackoverflow