Lint Rules

noExtraBooleanCast (since v0.9.0)

This rule is recommended by Rome.

Disallow unnecessary boolean casts

Examples

Invalid

if (!Boolean(foo)) {
}
complexity/noExtraBooleanCast.js:1:6 lint/complexity/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━

   Avoid redundant `Boolean` call
  
  > 1 │ if (!Boolean(foo)) {
        ^^^^^^^^^^^^
    2 │ }
    3 │ 
  
   It is not necessary to use `Boolean` call when a value will already be coerced to a boolean.
  
   Suggested fix: Remove redundant `Boolean` call
  
    1 │ if·(!Boolean(foo))·{
       --------   -   
while (!!foo) {}
complexity/noExtraBooleanCast.js:1:8 lint/complexity/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━

   Avoid redundant double-negation.
  
  > 1 │ while (!!foo) {}
          ^^^^^
    2 │ 
  
   It is not necessary to use double-negation when a value will already be coerced to a boolean.
  
   Suggested fix: Remove redundant double-negation
  
    1 │ while·(!!foo)·{}
         --       
let x = 1;
do {
1 + 1;
} while (Boolean(x));
complexity/noExtraBooleanCast.js:4:10 lint/complexity/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━

   Avoid redundant `Boolean` call
  
    2 │ do {
    3 │ 1 + 1;
  > 4 │ } while (Boolean(x));
            ^^^^^^^^^^
    5 │ 
  
   It is not necessary to use `Boolean` call when a value will already be coerced to a boolean.
  
   Suggested fix: Remove redundant `Boolean` call
  
    4 │ }·while·(Boolean(x));
           -------- -  
for (; !!foo; ) {}
complexity/noExtraBooleanCast.js:1:8 lint/complexity/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━

   Avoid redundant double-negation.
  
  > 1 │ for (; !!foo; ) {}
          ^^^^^
    2 │ 
  
   It is not necessary to use double-negation when a value will already be coerced to a boolean.
  
   Suggested fix: Remove redundant double-negation
  
    1 │ for·(;·!!foo;·)·{}
         --         
new Boolean(!!x);
complexity/noExtraBooleanCast.js:1:13 lint/complexity/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━

   Avoid redundant double-negation.
  
  > 1 │ new Boolean(!!x);
               ^^^
    2 │ 
  
   It is not necessary to use double-negation when a value will already be coerced to a boolean.
  
   Suggested fix: Remove redundant double-negation
  
    1 │ new·Boolean(!!x);
              --   

Valid

Boolean(!x);
!x;
!!x;