Jshint: Add option to suppress "Use '===' to compare with '0'" error

Created on 15 Nov 2013  ·  7Comments  ·  Source: jshint/jshint

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.

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.

All 7 comments

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NemoStein picture NemoStein  ·  7Comments

fbarda picture fbarda  ·  5Comments

nzakas picture nzakas  ·  10Comments

arian picture arian  ·  7Comments

voronianski picture voronianski  ·  8Comments