DEV Community

Cover image for Javascript Object #13
Asir-Sam
Asir-Sam

Posted on

Javascript Object #13

In this Post we are going to see the Object.is() method in Javascript.

Object.is()

This is method was introduced later in ECMASCRIPT17,this method is used to compare the two values.

The === and Object.is() behaves the same way as the Strict equal,but in two ways they differ.

  • -0 and +0

  • NaN

Negative zero

In === it treats both the negative and non negative numbers as same or else what we could say is, they are equal.
let's see this with an example,

`let amount = +0,
    volume = -0;
console.log(volume === amount);`
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

`true`
Enter fullscreen mode Exit fullscreen mode

Here comes the Object.is() he treat him as they are not same,

`let amount = +0,
    volume = -0;
console.log(Object.is(amount, volume));`
Enter fullscreen mode Exit fullscreen mode

OUTPUT

`false`
Enter fullscreen mode Exit fullscreen mode

Nan

Here comes another one,as when you compare the Nan with the === operator it will return false,as they think they are not same.Nan is the only number that does not equals itself.

`let quantity = NaN;
console.log(quantity === quantity);`
Enter fullscreen mode Exit fullscreen mode

OUTPUT

`false`
Enter fullscreen mode Exit fullscreen mode

However, Object.is() treats NaN as the same value:

`let quantity = NaN;

console.log(Object.is(quantity, quantity));


Output:

true`
Enter fullscreen mode Exit fullscreen mode

I'll attach a table that can help you a lot and save your many time.

Image description

Thanks for your mean Time,
Sam.

Top comments (0)