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);`
OUTPUT:
`true`
Here comes the Object.is() he treat him as they are not same,
`let amount = +0,
volume = -0;
console.log(Object.is(amount, volume));`
OUTPUT
`false`
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);`
OUTPUT
`false`
However, Object.is() treats NaN as the same value:
`let quantity = NaN;
console.log(Object.is(quantity, quantity));
Output:
true`
I'll attach a table that can help you a lot and save your many time.
Thanks for your mean Time,
Sam.
Top comments (0)