Object.is vs ===

ecmascript-6, javascript

Solution

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

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.

Problem

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

Original source