DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Why Object.is() is better than β€˜==’ and β€˜===’ ?
Maciej TrzciΕ„ski πŸŒ±πŸ‡΅πŸ‡±
Maciej TrzciΕ„ski πŸŒ±πŸ‡΅πŸ‡±

Posted on • Updated 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
 
prox_sea 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)..:)

Visualizing Promises and Async/Await 🀯

async await

☝️ Check out this all-time classic DEV post