Skip to content

noDoubleComparison (JavaScript)

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

Disallow two comparisons of the same values that can be combined into one.

When || or && joins two comparisons of the same two values, a single comparison often means the same thing. For example, x === y || x < y is the same as x <= y. A single comparison is shorter, easier to read, and evaluates each value only once.

The values can appear in either order: x === y || y < x is the same as x >= y.

The rule reports these pairs:

CodeCombined
x === y || x < yx <= y
x === y || x > yx >= y
x < y || x > yx !== y
x <= y && x >= yx === y
x <= y && x !== yx < y
x >= y && x !== yx > y

The rule only checks comparisons of variables, properties, this, and literal values such as numbers and strings. It ignores comparisons that use == or !=, call a function, or use optional chaining (?.).

The fix is unsafe because the combined comparison can give a different result when the two values are of different types, or when a value is undefined or NaN. For example, undefined === undefined || undefined < undefined is true, but undefined <= undefined is false. With x = "5", x === 5 || x < 5 is false, but x <= 5 is true.

x === y || x < y;
code-block.js:1:1 lint/nursery/noDoubleComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ These two comparisons can be combined into x <= y.

> 1 │ x === y || x < y;
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.

ℹ 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.

ℹ Unsafe fix: Combine them into a single comparison.

1 │ - x·===·y·||·x·<·y;
1 │ + x·<=·y;
2 2 │

x <= y && x >= y;
code-block.js:1:1 lint/nursery/noDoubleComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ These two comparisons can be combined into x === y.

> 1 │ x <= y && x >= y;
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.

ℹ 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.

ℹ Unsafe fix: Combine them into a single comparison.

1 │ - x·<=·y·&&·x·>=·y;
1 │ + x·===·y;
2 2 │

x <= y && x !== y;
code-block.js:1:1 lint/nursery/noDoubleComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ These two comparisons can be combined into x < y.

> 1 │ x <= y && x !== y;
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.

ℹ 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.

ℹ Unsafe fix: Combine them into a single comparison.

1 │ x·<=·y·&&·x·!==·y;
│ ------------
x === y || y < x;
code-block.js:1:1 lint/nursery/noDoubleComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ These two comparisons can be combined into x >= y.

> 1 │ x === y || y < x;
│ ^^^^^^^^^^^^^^^^
2 │

ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.

ℹ 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.

ℹ Unsafe fix: Combine them into a single comparison.

1 │ - x·===·y·||·y·<·x;
1 │ + x·>=·y;
2 2 │

x <= y;
x >= y;
x === y;
x < y;
x <= y || x < y;
x === y || x < z;