Skip to content

Latest commit

 

History

History
41 lines (24 loc) · 1.35 KB

no-constant-binary-operand.md

File metadata and controls

41 lines (24 loc) · 1.35 KB

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.

Rule Details

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: