DEV Community

Discussion on: Avoid boolean values comparison with "==" in JavaScript

 
mehraas profile image
Ashish Mehra

In some cases '==' is very useful. Like when we have to compare value (String)"10" with value 10(Integer).By using "==" in this case, we don't have to perform any explicit conversion.

Thread Thread
 
gsonderby profile image
Gert Sønderby

No, do the explicit conversion, then compare strictly. Otherwise you are leaving yourself open to bugs that will be hard to detect. There's literally no case where == is desirable.

Thread Thread
 
mehraas profile image
Ashish Mehra

Thanks for making the point. :-)

Thread Thread
 
staskjs profile image
Stanislav Karpov

The only case when I use == is comparing to null, that also checks for undefined. It is an extremely rare when you need to check for undefined and null independently

Thread Thread
 
mehraas profile image
Ashish Mehra • Edited

I prefer Object.is() to compare NaN and for undefined I just pass variable inside of condition because undefined is falsey value.

Thread Thread
 
icatalina profile image
Ignacio Catalina

If you need to check for undefined and null just do

if (variable === undefined || variable === null) {

if I see code doing:

if (variable == null) {

I have no way to know if you actually wanted to check only for null or null and undefined.

It is always better to be explicit.