noNegatedComparison (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noNegatedComparison - This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-negated-comparison
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNegatedComparison": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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)) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.