A == and! = Becoming a bad practice?

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?

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

While this is falsey:

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » A == and! = Becoming a bad practice?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录