Using ==
to compare to zero simply isn't an error or dangerous when comparing a value that is guaranteed to be a Number. For example:
var arr = ["foo"];
var arrIsEmpty = (arr.length == 0);
I tend to use ==
in these situations and it is a purely stylistic decision. I would like JSHint to respect my well-informed wishes.
You're aware of the eqeqeq
option, right? That is where this error comes from, setting it to false will free you from this case. Or are you looking for something more specific, like only suppressing the comparisons to 0
(and not comparisons to other values)?
eqeqeq is the general case. There seems to be a separate check for comparing to 0 that complains even if eqeqeq is false.
What I really want is to be able to suppress all forms of complaints from JSHint about using == and !=, but it's the comparison to 0 that prevents that.
Hmm I see. Related code is here, it's definitely specifically calling that comparison out (among others):
https://github.com/jshint/jshint/blob/6140ade7437799e182347bf2d39ec68c3c3d0263/src/jshint.js#L2201-L2215
https://github.com/jshint/jshint/blob/6140ade7437799e182347bf2d39ec68c3c3d0263/src/jshint.js#L1338-L1346
You can disable this warning by its code.
For others who may have a hard time to make use of valueof's answer, setting "-W041": false
seems to do the trick.
Thanks @privman
FYI, I also found more details here
Is there a motivation for keeping W041
warnings in effect when "eqeqeq": false
?
They effectively prohibit use of ==
and !=
in affected situations.
Most helpful comment
For others who may have a hard time to make use of valueof's answer, setting
"-W041": false
seems to do the trick.