Compare Value To Itself

ID

javascript.compare_value_to_itself

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

CWE:570, CWE:571, reliability, suspicious-comparison

Description

Reports comparisons where both sides are the same expression — x === x, a > a, obj.field ⇐ obj.field, etc. The result is constant for any value of the operand and is almost always a copy-paste mistake or a leftover from a refactor.

Rationale

Self-comparisons are at best dead code, at worst a hidden logic bug:

  • x === x is always true for any value except NaN (where it is false); x !== x is the inverse.

  • Relational self-comparisons (a > a, a >= a, a < a, a ⇐ a) are constant for non-NaN values.

  • The intent was almost certainly to compare two different operands.

The single legitimate use of x !== x — testing for NaN — is better expressed via Number.isNaN(x). The dedicated javascript.use_isnan rule guides that fix; this rule flags the symptomatic pattern uniformly so neither idiom slips through.

// Bad - constant outcomes from self-comparison
if (count === count) { ... }
if (status !== status) { ... }      // intent was probably Number.isNaN(status)
if (price > price) { ... }

Remediation

Replace the duplicate operand with the value it should be compared against, or — for the NaN case — use Number.isNaN(…​).

// Good - real comparison
if (count === expectedCount) { ... }
if (Number.isNaN(status)) { ... }
if (price > minPrice) { ... }