DEV Community

Cover image for Why Object.is() is better than ‘==’ and ‘===’ ?
Maciej Trzciński 🌱🇵🇱
Maciej Trzciński 🌱🇵🇱

Posted on • Edited on • Originally published at Medium

Why Object.is() is better than ‘==’ and ‘===’ ?

Everybody who used JavaScript knows that JS is loosely typed and comparing with ‘==’ gives unexpected results.

So if we use triple equals operator ‘===’, we got a more strict result, but it is also not yet perfect:

For help us in this case we use ES6 ‘Object.is()’, it is more precision than ‘===’ and moreover it behaves well in some special cases:

Compare table:

Table

Polyfill:

If you like this article, follow me on Twitter @MaciejDEV

Top comments (4)

Collapse
 
codefinity profile image
Manav Misra

I had 4gotten about this. === really is 'good enough' - less typing than Object.is - Besides those edge cases, NS 😕 if it's worth it?
GTK, though. JIC.

Collapse
 
aqrojo profile image
aqrojo

Yes, all that tests return false when you use '==='

' ' == 0 //false
null == undefined //false
[1] == true //false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zeedu_dev profile image
Eduardo Zepeda

Thank you so much for your post!

Collapse
 
sidthesloth92 profile image
Dinesh Balaji

Nice post. I believe you have misplaced === code snippet with == (The second snippet)..:)