Skip to content

Latest commit

 

History

History
66 lines (40 loc) · 2.13 KB

no-constant-binary-expression.md

File metadata and controls

66 lines (40 loc) · 2.13 KB

disallow expressions where the operation doesn’t affect the value (no-constant-binary-expression)

Comparisons which will always evaluate to true or 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. For example:

// One might think this would evaluate as `x + (b ?? c)`:
const x = a + b ?? c;

// But it actually evaluates as `(a + b) ?? c`. Since `a + b` can never be null,
// the `?? c` has no effect.

Additionally, we detect comparisons to newly constructed objects/arrays/functions/etc. In JavaScript, where objects are compared by reference, a newly constructed object can never === any other value. This can be surprising for programmers coming from languages where objects are compared by value.

// Programmers coming from a language where objects are compared by value might expect this to work:
const isEmpty = x === [];

// However, this will always result in `isEmpty` being `false`.

Rule Details

This rule identifies == and === comparisons which, based on the semantics of the JavaScript language, will always evaluate to 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-expression: "error"*/
const value1 = +x == null;

const value2 = condition ? x : {} || DEFAULT;

const value3 = !foo == null;

const value4 = new Boolean(foo) === true;

const arrIsEmpty = someObj === {};

const arrIsEmpty = someArr === [];

Examples of correct code for this rule:

/*eslint no-constant-binary-expression: "error"*/
const value1 = x == null;

const value2 = (condition ? x : {}) || DEFAULT;

const value3 = !(foo == null);

const value4 = Boolean(foo) === true;

const arrIsEmpty = Object.keys(someObj).length === 0;

const arrIsEmpty = someArr.length === 0;

Related Rules: