Skip to content

noNegatedComparison (JavaScript)

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

Disallow negating a whole equality comparison.

Code such as !(a === b) makes the reader work out the comparison first and then reverse the result. The opposite operator gives the same result and reads more directly: a !== b.

This rule checks the equality operators ===, !==, ==, and !=. Negated <, >, <=, and >= comparisons are allowed, because the opposite operator doesn’t always give the same result. For example, !(value > 0) is true when value is NaN or undefined, but value <= 0 is false.

const isDifferent = !(a === b);
code-block.js:1:21 lint/nursery/noNegatedComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This comparison is negated as a whole.

> 1 │ const isDifferent = !(a === b);
│ ^^^^^^^^^^
2 │

ℹ Reading it means working out the comparison and then reversing the result.

ℹ Use the opposite operator !== instead, which gives the same result.

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

ℹ Safe fix: Use !== instead.

1 │ - const·isDifferent·=·!(a·===·b);
1 │ + const·isDifferent·=·a·!==·b;
2 2 │

if (!(typeof value === "undefined")) {}
code-block.js:1:5 lint/nursery/noNegatedComparison  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This comparison is negated as a whole.

> 1 │ if (!(typeof value === "undefined")) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Reading it means working out the comparison and then reversing the result.

ℹ Use the opposite operator !== instead, which gives the same result.

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

ℹ Safe fix: Use !== instead.

1 │ - if·(!(typeof·value·===·"undefined"))·{}
1 │ + if·(typeof·value·!==·"undefined")·{}
2 2 │

const isDifferent = a !== b;
if (typeof value !== "undefined") {}
if (!(a === b && c === d)) {}
if (!(a > b)) {}