Skip to content

no-constant-comparison

Reports an equality or a comparison whose two sides are both literals (1 === 2, "a" < "b"). An operand that is an expression of literals (1 + 1 === 2) is not reported.

The result of a comparison of two literals is fixed when the code is written, so one branch of the if never runs. It is usually a leftover of debugging, or of a value that was meant to be a variable.

To fix, compare the variable, or replace the test with its result.

Reported
...
if (1 === 2) {} if (a === 1) {}
...
c:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
cpp:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
csharp:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
java:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
js:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
kotlin:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
php:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
python:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error
ruby:
lint:
rules:
no-constant-comparison: warn # off, warn (default), error

Enabled by the presets: js/eslint-recommended, js/oxlint.

Suppress one line with sudsy-disable-next-line no-constant-comparison in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-constant-binary-expression partial only two literals compared