Skip to main content

No Boolean Var For If

Inline the condition directly in the if-statement instead of extracting it into a single-use boolean variable.

Summary

The no-boolean-var-for-if rule reports a const declaration with a LogicalExpression initializer (e.g., &&, ||) when the variable is used exactly once as the test of the immediately following if-statement.

Variables used more than once or in a different context are not affected.

Why Use This Rule?

  1. A single-use boolean variable adds an indirection that makes the reader jump between two lines to understand the condition.
  2. Inlining the condition keeps the logic visible at the point of decision.
  3. Reduces unnecessary variable declarations that exist only to name a condition used once.

Examples

Configuration

Options

warning

ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.

OptionTypeDefaultDescription
ignoreFilesstring[][]File patterns to skip.

Autofix

Autofix is not available for this rule. Flagged code must be updated manually.

Troubleshooting

  • Warning does not fire when the variable is used twice — The rule uses scope analysis and only flags variables that are read exactly once. If the variable is referenced in multiple places, it is not flagged.
  • Warning does not fire on a non-logical initializer — Only LogicalExpression initializers (&&, ||, ??) trigger the rule. Simple comparisons or function calls are not affected.
  • Warning fires on a const with let — The rule only checks const declarations. let declarations are not affected.