Skip to content

noSelfCompare

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noSelfCompare": "error"
}
}
}
}

Disallow comparisons where both sides are exactly the same.

Comparing a variable against itself is usually an error, either a typo or refactoring error. It is confusing to the reader and may potentially introduce a runtime error.

The only time you would compare a variable against itself is when you are testing for NaN. However, it is far more appropriate to use typeof x === 'number' && Number.isNaN(x) for that use case rather than leaving the reader of the code to determine the intent of self comparison.

if (x === x) {}
code-block.js:1:5 lint/suspicious/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This comparison uses the same expression on both sides.

> 1 │ if (x === x) {}
^^^^^^^
2 │

Self-comparisons are usually redundant or a sign that the wrong value is being compared.

Compare two different values instead, or use Number.isNaN() if you are checking for NaN.

if (a.b.c() !== a.b .c()) {}
code-block.js:1:5 lint/suspicious/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This comparison uses the same expression on both sides.

> 1 │ if (a.b.c() !== a.b .c()) {}
^^^^^^^^^^^^^^^^^^^^
2 │

Self-comparisons are usually redundant or a sign that the wrong value is being compared.

Compare two different values instead, or use Number.isNaN() if you are checking for NaN.