Skip to content

noSubtractionComparison (JavaScript)

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

Disallow subtracting two values only to compare the result with 0.

Code such as a - b > 0 subtracts one value from another, then checks whether the difference is above, below, or equal to 0. Comparing the two values directly, as in a > b, is shorter and states the intent more clearly.

This pattern often remains after code is copied from a sort comparison function such as (a, b) => a - b.

The rule checks the operators >, >=, <, <=, ===, !==, ==, and !=, with 0 on either side. When 0 is on the left, the fix flips the operator, so 0 < a - b becomes a > b.

if (a - b > 0) {}
code-block.js:1:5 lint/nursery/noSubtractionComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This comparison subtracts two values and compares the difference with 0.

> 1 │ if (a - b > 0) {}
│ ^^^^^^^^^
2 │

ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.

ℹ 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: Compare the values directly.

1 │ - if·(a·-·b·>·0)·{}
1 │ + if·(a·>·b)·{}
2 2 │

if (a - b <= 0) {}
code-block.js:1:5 lint/nursery/noSubtractionComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This comparison subtracts two values and compares the difference with 0.

> 1 │ if (a - b <= 0) {}
│ ^^^^^^^^^^
2 │

ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.

ℹ 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: Compare the values directly.

1 │ - if·(a·-·b·<=·0)·{}
1 │ + if·(a·<=·b)·{}
2 2 │

if (0 < a - b) {}
code-block.js:1:5 lint/nursery/noSubtractionComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This comparison subtracts two values and compares the difference with 0.

> 1 │ if (0 < a - b) {}
│ ^^^^^^^^^
2 │

ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.

ℹ 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: Compare the values directly.

1 │ - if·(0·<·a·-·b)·{}
1 │ + if·(a·>·b)·{}
2 2 │

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

⚠ This comparison subtracts two values and compares the difference with 0.

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

ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.

ℹ 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: Compare the values directly.

1 │ - if·(a·-·b·===·0)·{}
1 │ + if·(a·===·b)·{}
2 2 │

if (a > b) {}
if (a - b > 1) {}
if (a + b > 0) {}
if (a - b > 0n) {}