# no-extra-boolean-cast

Disallow unnecessary boolean casts.

In contexts where expression will be coerced to a Boolean (e.g. if), casting to a boolean (using !! or Boolean(expr)) is unnecessary.

# Invalid Code Examples

if (!!foo) {}
while (!!foo) {}

var foo = !!!bar;
var foo = Boolean(!!bar);

# Config

Name Type Description
enforceForLogicalOperands bool If this option is true, this rule will also check for unnecessary boolean
cast inside logical expression, which is disabled by default.
More incorrect examples
if (!!foo) {}
do {} while (!!foo)
while (!!foo) {}
!!foo ? bar : baz
for (; !!foo;) {}
!!!foo
Boolean(!!foo)
new Boolean(!!foo)
if (Boolean(foo)) {}
do {} while (Boolean(foo))
while (Boolean(foo)) {}
Boolean(foo) ? bar : baz
for (; Boolean(foo);) {}
!Boolean(foo)
!Boolean(foo && bar)
!Boolean(foo + bar)
!Boolean(+foo)
!Boolean(foo())
!Boolean(foo = bar)
!Boolean(...foo);
!Boolean(foo, bar());
!Boolean((foo, bar()));
!Boolean();
!(Boolean());
if (!Boolean()) { foo() }
while (!Boolean()) { foo() }
if (Boolean()) { foo() }
while (Boolean()) { foo() }
Boolean(Boolean(foo))
Boolean(!!foo, bar)
More correct examples
Boolean(bar, !!baz);
var foo = !!bar;
function foo() { return !!bar; }
var foo = bar() ? !!baz : !!bat
for(!!foo;;) {}
for(;; !!foo) {}
var foo = Boolean(bar);
function foo() { return Boolean(bar); }
var foo = bar() ? Boolean(baz) : Boolean(bat)
for(Boolean(foo);;) {}
for(;; Boolean(foo)) {}
if (new Boolean(foo)) {}

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM