Skip to content

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.

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]) {
}

This rule is not configurable.

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)
Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.