Empty Loop Body

ID

javascript.empty_loop_body

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

CWE:1071, reliability, suspicious-comparison

Description

Reports for, while, do-while, and for-in/of loops whose body is empty — a bare semicolon, an empty block, or a body that contains only nested empty statements. The most common cause is a stray semicolon between the loop header and the intended body.

Rationale

An empty loop body is almost always a bug:

  • A stray ; after the loop header turns what looks like the loop body into a separate statement that runs only once, and the loop spins or iterates without effect.

  • A copy-paste leftover or a comment-only body that no longer exists silently discards the loop’s purpose.

  • The very rare legitimate use — a busy-wait such as while (atomicCheck()); — is itself a code smell that deserves a comment and ideally a different synchronisation primitive.

// Bad - stray semicolon: the loop body never executes meaningful work
for (let i = 0; i < items.length; i++); // intended block was below
console.log("done");

Remediation

Remove the stray punctuation, or place real work inside the body. If the loop is intentionally empty (e.g. a polling spin), refactor to an explicit asynchronous wait or document the intent with a comment.

// Good
for (let i = 0; i < items.length; i++) {
  items[i] = items[i] * 2;
}