Search code examples
javascriptjshint

How to use ! non-confusingly?


My JavaScript code features an if statement with the following (IMHO very simple) condition:

if (a !== !!b) {
}

Now, JSHint complains as follows, while marking the first exclamation mark in front of b:

JSHint: Confusing use of '!'. (W018)

How do I change this line to be non-confusing (as defined by JSHint) without changing its semantics, and without splitting it up into several instructions?


To be clear: a is a boolean, while b may or may not be a boolean.


Solution

  • Would it be that the ubiquitous JS idiom, !! for boolean conversion, is confusing to JSHint? If so: 1) maybe it can be turned off in the config, 2) you can try the more verbose if (a !== Boolean(b)).