noChainedComparison (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noChainedComparison - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
oxc/bad-comparison-sequence
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noChainedComparison": "error" } } }}Description
Section titled “Description”Disallow chaining comparisons, as in a < b < c.
A comparison such as a < b always produces true or false. When you write
a < b < c, JavaScript first compares a with b, and then compares that true or
false with c. It never compares b with c. For example, 3 > 2 > 1 is false,
because 3 > 2 is true, and true > 1 is false.
The same happens with equality checks: a === b === c compares a with b, and then
compares the result with c. It doesn’t check that all three values are equal.
To compare each pair of values, join separate comparisons with &&, as in
a < b && b < c.
The rule reports two comparisons in a row that both use <, >, <=, or >=, or that
both use ==, ===, !=, or !==. Parentheses don’t change the result, so
(a === b) === c is also reported.
The rule ignores an equality check when the value on its right is also true or
false: the true and false literals, another comparison, or an expression that
starts with !. Checking whether two booleans are equal is usually intended, as in
(a === null) === (b === null).
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (a < b < c) {}code-block.js:1:5 lint/nursery/noChainedComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares the result of another comparison with a value.
> 1 │ if (a < b < c) {}
│ ^^^^^^^^^
2 │
ℹ This comparison runs first and produces true or false.
> 1 │ if (a < b < c) {}
│ ^^^^^
2 │
ℹ This value is then compared with that true or false, not with the values of the first comparison.
> 1 │ if (a < b < c) {}
│ ^
2 │
ℹ For example, 3 > 2 > 1 is false, because 3 > 2 is true, and true > 1 is false.
ℹ To compare each pair of values, join separate comparisons with &&, as in a < b && b < c.
ℹ 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 === c) {}code-block.js:1:5 lint/nursery/noChainedComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares the result of another comparison with a value.
> 1 │ if (a === b === c) {}
│ ^^^^^^^^^^^^^
2 │
ℹ This comparison runs first and produces true or false.
> 1 │ if (a === b === c) {}
│ ^^^^^^^
2 │
ℹ This value is then compared with that true or false, not with the values of the first comparison.
> 1 │ if (a === b === c) {}
│ ^
2 │
ℹ For example, 2 === 2 === 2 is false, because 2 === 2 is true, and true === 2 is false.
ℹ To compare each pair of values, join separate comparisons with &&, as in a === b && b === c.
ℹ 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) != c) {}code-block.js:1:5 lint/nursery/noChainedComparison ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This compares the result of another comparison with a value.
> 1 │ if ((a == b) != c) {}
│ ^^^^^^^^^^^^^
2 │
ℹ This comparison runs first and produces true or false.
> 1 │ if ((a == b) != c) {}
│ ^^^^^^
2 │
ℹ This value is then compared with that true or false, not with the values of the first comparison.
> 1 │ if ((a == b) != c) {}
│ ^
2 │
ℹ For example, 2 === 2 === 2 is false, because 2 === 2 is true, and true === 2 is false.
ℹ To compare each pair of values, join separate comparisons with &&, as in a === b && b === c.
ℹ 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 < c) {}if (a === b && b === c) {}if (a < b === c) {}if ((a === null) === (b === null)) {}if ((a === b) === false) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.