noSelfCompare (since v12.0.0)
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 usetypeof x === 'number' && isNaN(x)
or the Number.isNaN ES2015 function for that use case rather than leaving the reader of the code to determine the intent of self comparison.
Source: no-self-compare.
Examples
Invalid
if (x === x) {}
nursery/noSelfCompare.js:1:5 lint/nursery/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Comparing to itself is potentially pointless.
> 1 │ if (x === x) {}
│ ^^^^^^^
2 │
if (a.b.c() !== a.b .c()) {}
nursery/noSelfCompare.js:1:5 lint/nursery/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Comparing to itself is potentially pointless.
> 1 │ if (a.b.c() !== a.b .c()) {}
│ ^^^^^^^^^^^^^^^^^^^^
2 │