Useful JS Tips

#68 - Why you should use in equality comparison

2017-04-03 by @TarekAlQaddy

We all know that JavaScript is loosely typed and in some cases it fall behind specially when it comes to quality comparison with ‘==’, comparing with ‘==’ gives unexpected results due to whats called coercion or casting “converting one of the 2 operands to the other’s type then compare”.

0 == ' ' //true
null == undefined //true
[1] == true //true

So they provided us with the triple equal operator ‘===’ which is more strict and does not coerce operands, However comparing with ‘===’ is not the best solution you can get:

NaN === NaN //false

The great news that in ES6 there is the new ‘’ which is better and more precise it has the same features as ‘===’ and moreover it behaves well in some special cases: , ' '); //false, undefined); //false[1], true); //false, NaN); //true

Mozilla team doesn’t think that is “stricter” than ‘===’ they say that we should think of how this method deal with NaN, -0 and +0 but overall I think it is now a good practice in real applications.

Now this table illustrates…

differences of operators in equality comparisons javascript


Equality comparisons and sameness