selfComparisons
Reports comparing a value to itself.
✅ This rule is included in the ts logical
preset.
Comparing a value to itself (e.g., x === x
, y < y
) always produces the same result and is likely a mistake.
This pattern often indicates a copy-paste error or typo where different variables were intended to be compared.
Self-comparisons always evaluate to the same boolean value for a given operator.
For example, x === x
is always true
(except for NaN
), while x < x
is always false
.
This rule reports on binary comparison expressions where both operands are identical.
Examples
Section titled “Examples”if (value === value) { // Always true (except for NaN)}
if (value < value) { // Always false}
// Always true (except for NaN)const result = object.property === object.property;
// Always false (except for NaN)if (array[0] !== array[0]) {}
// Compare different valuesif (value === otherValue) { // ...}
if (value < maxValue) { // ...}
// Compare different propertiesconst result = object.property === object.otherProperty;
// Compare different array elementsif (array[0] !== array[1]) { // ...}
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project uses control flow structures that create confusing side effects, such as dynamic getters that modify state, you might not be able to enable this rule. Consider refactoring to more predictable control flow structures.
The only legitimate use case for self-comparison is checking for NaN
(since NaN !== NaN
), but modern JavaScript provides better methods for this:
Number.isNaN(value)
(preferred)isNaN(value)
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noSelfCompare
- Deno:
no-self-compare
- ESLint:
no-self-compare
- Oxlint:
eslint/no-self-compare