loverajoel / jstips

This is about useful JS tips!
http://jstips.co
GNU General Public License v3.0
12.5k stars 804 forks source link

Create Why you should use Object.is() in Equality Comparison in JavaScript #405

Closed TarekAlQaddy closed 7 years ago

TarekAlQaddy commented 7 years ago

Why you should use Object.is() in Equality Comparison in JavaScript

TL;DR;

a good solution for the looseness of equality comparisons in javascript

TarekAlQaddy

@TarekAlQaddy

loverajoel commented 7 years ago

Nice tips! Thanks @TarekAlQaddy 👍

TarekAlQaddy commented 7 years ago

Thanks @loverajoel

kurtextrem commented 7 years ago

Thank you for the contribution, however, is this a tip? For me it seems like this is more useful for edge cases. In addition, this might have lower perf as it is a function call vs. native syntax. For newbies that are subscribed to jstips this might look like "always use Object.is instead of ===".

TarekAlQaddy commented 7 years ago

@kurtextrem Thanks for your interest, When the native syntax lacks that kind of strictness, The function call would be a good replacement and I think that's why they made Object.is in ES6. for newbies what I meant that if they only want a strict version of comparisons they should go with it yes. in java for example we sometimes use Object.equals in comparisons.

0x1eef commented 7 years ago

besides NaN, are there other cases Object.is handles that === does not?

TarekAlQaddy commented 7 years ago

@r-obert as I mentioned there is also -0 === +0 which gives true but by Object.is it's false

You can check Mozilla reference http://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

0x1eef commented 7 years ago

thanks @TarekAlQaddy