π Disallow unnecessary operands in logical expressions involving boolean literals.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Logical && and || expressions return one of their operands. A boolean literal can make part of the expression unnecessary, but it is not always safe to simplify. For example, value && true returns true for any truthy non-boolean value, while value would return the original value.
This rule only reports boolean literal operands when the simplification preserves the logical expression value, or when the expression is used only for truthiness. It ignores directive prologue cases where simplifying to a string literal could change strict-mode semantics, and autofixes are skipped when comments could be affected.
It intentionally does not report non-leading absorbing operands like value && false or value || true. Those expressions still evaluate value, so replacing the whole expression with a literal could drop side effects or thrown errors. Use ESLint's no-constant-binary-expression rule if you want those suspicious expressions reported without an autofix.
// β
const value = true && input;
const other = input && true && fallback;
// β
const value = input;
const other = input && fallback;// β
if (input && true) {}
const value = Boolean(input || false);
// β
if (input) {}
const value = Boolean(input);// β
const value = false && input;
const other = true || input;
// β
const value = false;
const other = true;// Not reported by this rule
const value = input && true;
const other = input || false;
const alwaysFalse = input && false;
const alwaysTrue = input || true;