Skip to content

noConstantComparisons (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noConstantComparisons": "error"
}
}
}
}

Disallow conditions that are always true, can never be true, or contain a check with no effect.

The rule reports && and || expressions in these cases:

  • Two comparisons of the same value against numbers can never both be true, as in x <= 400 && x > 500. The whole condition is always false.
  • One comparison already implies the other, as in x < 200 && x <= 299. Every value below 200 is also at most 299, so the second comparison has no effect.
  • Both sides make the same check, as in a === b && b === a or !done || !done.
  • One side is the opposite of the other, as in a === b && a !== b or ready || !ready. With &&, the condition can never be true. With ||, it is always true.

Such conditions are usually mistakes: a flipped comparison operator, a wrong number, or the wrong variable.

The first two cases only apply to &&, and only to comparisons against numbers written directly in the code, such as 500 or -1. In a chain such as x > 5 && isReady && x < 3, the rule compares the last comparison with the earlier comparisons in the chain. It stops at a part of the chain that assigns a new value, such as (x = 10), because the earlier comparisons may have checked another value.

The rule treats two values as the same when they are the same variable, property, this, or literal. It never treats two function calls as the same value, because each call can return something different.

if (statusCode <= 400 && statusCode > 500) {}
code-block.js:1:5 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This condition can never be true.

> 1 │ if (statusCode <= 400 && statusCode > 500) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ No value can make this comparison...

> 1 │ if (statusCode <= 400 && statusCode > 500) {}
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ ...and this comparison true at the same time.

> 1 │ if (statusCode <= 400 && statusCode > 500) {}
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Check whether a comparison operator or a number is wrong.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (statusCode < 200 && statusCode <= 299) {}
code-block.js:1:25 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison has no effect.

> 1 │ if (statusCode < 200 && statusCode <= 299) {}
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ Whenever this comparison is true, the other comparison is also true.

> 1 │ if (statusCode < 200 && statusCode <= 299) {}
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Remove the comparison that has no effect, or check whether a comparison operator or a number is wrong.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (statusCode > 500 && statusCode >= 500) {}
code-block.js:1:25 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This comparison has no effect.

> 1 │ if (statusCode > 500 && statusCode >= 500) {}
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ Whenever this comparison is true, the other comparison is also true.

> 1 │ if (statusCode > 500 && statusCode >= 500) {}
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Remove the comparison that has no effect, or check whether a comparison operator or a number is wrong.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (a === b && b === a) {}
code-block.js:1:5 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ Both sides of this logical expression make the same check.

> 1 │ if (a === b && b === a) {}
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ Repeating a check doesn't change the result, so one side has no effect.

ℹ Remove one side, or check whether one side should check something else.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (a === b && a !== b) {}
code-block.js:1:5 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This condition can never be true.

> 1 │ if (a === b && a !== b) {}
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ One side is the opposite of the other, so they can never both be true.

ℹ Check whether one side should check something else.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (ready || !ready) {}
code-block.js:1:5 lint/nursery/noConstantComparisons ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This condition is always true.

> 1 │ if (ready || !ready) {}
│ ^^^^^^^^^^^^^^^
2 │

ℹ One side is the opposite of the other, so one of them is always true.

ℹ Check whether one side should check something else.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (statusCode >= 400 && statusCode < 500) {}
if (500 <= statusCode && 600 > statusCode) {}
if (statusCode >= 500 && statusCode <= 500) {}
if (a === b && b === c) {}
if (ready || !done) {}
  • If you want to disallow comparing a value with itself, such as a < a, see noSelfCompare.