disallow constant comparisons and expressions that always/never short circuit (no-constant-binary-operand)
Comparisons which either will always evaluates to true, or always evaluates to false, and logical expressions (||
, &&
, ||
) which either always short circuit or never short circuit are both likely indications of programmer error.
These errors are especially common in complex expressions where operator precedence is easy to misjudge.
This rule identifies ==
and ===
comparisons which, based on the semantics of the JavaScript language, will always evaluate a constant true
or false
.
It also identifies ||
, &&
and ??
logical expressions which will either always, or never short circuit.
Examples of incorrect code for this rule:
/*eslint no-constant-binary-operand: "error"*/
const value1 = +x == null;
const value2 = condition ? x : {} || DEFAULT;
const value3 = !foo == null;
const value4 = new Boolean(foo) === true
Examples of correct code for this rule:
/*eslint no-constant-binary-operand: "error"*/
const value1 = x == null;
const value2 = (condition ? x : {}) || DEFAULT;
const value3 = !(foo == null);
const value4 = Boolean(foo) === true
See Also:
no-constant-condition
which disallows constant values as test conditions.