Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has == and != become bad practise?

Since a few months, my IDE (WebStorm) highlights JavaScript regular equality operators with the following warning:

Comparioson a == b may cause unexpected type coercion.
This inspection reports usages of JavaScript quality operators which may cause unexpected
type coercions. It is considered a good practice to use the type-safe equality operators
=== and !== instead of their regular counterparts == and !=.

I am aware of the different behavours of both operators and I tend to use them because of their different behavour, eg. for lazy type conversions:

if(parseInt(val) == val) // val can be safely converted to int

However, the IDE is adding warnings to all occurences of ==, so the above does not feel right anymore. I'm could convert all these parts into something much less readable:

if(parseInt(val).toString() === val) // be happy webstorm

Is this really the way to go; or should I rather ignore/disable these warnings?

like image 497
RienNeVaPlu͢s Avatar asked Sep 02 '25 05:09

RienNeVaPlu͢s


2 Answers

Yes, this has been best practice for almost two decades.

The warning is quite clear (even explaining why it's in place), and you'll find the same advice in your JavaScript books as well as widely across the web.

So, I can't comprehend why you'd consider ignoring or even disabling it.


You can find more information on when to pick == and when to pick === here:

  • When is it OK to use == in JavaScript?
like image 116
Lightness Races in Orbit Avatar answered Sep 04 '25 19:09

Lightness Races in Orbit


I would argue that any form of type coercion, if unexpected for types that may not be known at runtime (like any dynamic language) is bad practice.

For example, these are both truthy:

 "0" == false
 "0"

While this is falsey:

 false
like image 27
Alexander Huszagh Avatar answered Sep 04 '25 19:09

Alexander Huszagh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!