In JavaScript, every value can be coerced into a boolean value: either true
or false
. Values that are coerced into
true
are said to be truthy, and those coerced into
false
are said to be falsy.
Explicit conversion to a boolean can be done with double negation (!!
) or a Boolean
call. Depending on the context, this
may be redundant as JavaScript uses implicit type coercion and automatically converts values to booleans when used with logical operators, conditional
statements, or any boolean context.
if (!!foo) { // Noncompliant: redundant '!!'
// ...
}
if (Boolean(foo)) { // Noncompliant: redundant 'Boolean' call
// ...
}
A redundant boolean cast affects code readability. Not only the condition becomes more verbose but it also misleads the reader who might question
the intent behind the extra cast. The condition can be written without the Boolean cast.
if (foo) {
// ...
}