Skip to content

no-constant-condition

Reports the condition of an if, a ternary or a loop that is a literal (if (true), if (0), x = true ? 1 : 2, while (false)). The usual forms of an endless loop (while (true), while (1), for (;true;), do {} while (true)) are left alone.

With a literal as the test one branch always runs and the other never does, so the test is either dead code or a switch that was left on while debugging.

To fix, remove the test and keep the branch that runs, or put the real condition in.

Reported
...
if (true) {} while (0) {} while (true) {} for (;;) {} var c = "x" ? 1 : 2, d = a ? 1 : 2;
c:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
cpp:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
csharp:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
go:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
java:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
js:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
kotlin:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
php:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
python:
lint:
rules:
no-constant-condition: warn # off, warn (default), error
ruby:
lint:
rules:
no-constant-condition: warn # off, warn (default), error

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

Suppress one line with sudsy-disable-next-line no-constant-condition 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-condition partial literals only (no object, function or ! forms)