Skip to content

noConstantBinaryExpression

Disallow expressions where the operation doesn’t affect the value

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.

const value1 = +x == null;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This binary expression leads to a constant result.

> 1 │ const value1 = +x == null;
^^^^^^^^^^
2 │

const value2 = condition ? x : {} || DEFAULT;
code-block.js:1:32 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This logical expression can be simplified.

> 1 │ const value2 = condition ? x : {} || DEFAULT;
^^^^^^^^^^^^^
2 │

This operand always evaluates to the same truthiness.

> 1 │ const value2 = condition ? x : {} || DEFAULT;
^^
2 │

const value3 = !foo == null;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This binary expression leads to a constant result.

> 1 │ const value3 = !foo == null;
^^^^^^^^^^^^
2 │

const value4 = new Boolean(foo) === true;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This binary expression leads to a constant result.

> 1 │ const value4 = new Boolean(foo) === true;
^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

const objIsEmpty = someObj === {};
code-block.js:1:20 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected comparison to newly constructed object.

> 1 │ const objIsEmpty = someObj === {};
^^^^^^^^^^^^^^
2 │

This expression always constructs a new object.

> 1 │ const objIsEmpty = someObj === {};
^^
2 │

const arrIsEmpty = someArr === [];
code-block.js:1:20 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected comparison to newly constructed object.

> 1 │ const arrIsEmpty = someArr === [];
^^^^^^^^^^^^^^
2 │

This expression always constructs a new object.

> 1 │ const arrIsEmpty = someArr === [];
^^
2 │

const shortCircuit1 = condition1 && false && condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This logical expression can be simplified.

> 1 │ const shortCircuit1 = condition1 && false && condition2;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This operand always evaluates to the same truthiness.

> 1 │ const shortCircuit1 = condition1 && false && condition2;
^^^^^^^^^^^^^^^^^^^
2 │

const shortCircuit2 = condition1 || true || condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This logical expression can be simplified.

> 1 │ const shortCircuit2 = condition1 || true || condition2;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This operand always evaluates to the same truthiness.

> 1 │ const shortCircuit2 = condition1 || true || condition2;
^^^^^^^^^^^^^^^^^^
2 │

const shortCircuit3 = condition1 ?? "non-nullish" ?? condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This logical expression can be simplified.

> 1 │ const shortCircuit3 = condition1 ?? “non-nullish” ?? condition2;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This operand always evaluates to the same nullishness.

> 1 │ const shortCircuit3 = condition1 ?? “non-nullish” ?? condition2;
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

const value1 = x == null;
const value2 = (condition ? x : {}) || DEFAULT;
const value3 = !(foo == null);
const value4 = Boolean(foo) === true;
const objIsEmpty = Object.keys(someObj).length === 0;
const arrIsEmpty = someArr.length === 0;
biome.json
{
"linter": {
"rules": {
"nursery": {
"noConstantBinaryExpression": "error"
}
}
}
}