Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.is vs ===

People also ask

What is the primary difference between object is and the == operator?

The only difference between Object.is() and === is in their treatment of signed zeroes and NaNs. For example, the === operator (and the == operator) treats the number values -0 and +0 as equal. Also, the === operator treats Number. NaN and NaN as not equal.

What is the difference between == and === while comparing objects?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What does === means?

The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.

Why would you use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...


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


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

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

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