Default Clause Not Last

ID

javascript.switch_default_not_last

Severity

high

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

JavaScript

Tags

code-style, reliability

Description

Reports a switch whose default clause is not its last clause. A switch with no default at all is out of scope — javascript.default_case covers that.

Rationale

// Bad
switch (level) {
  default:
    log("unknown level");    // falls through into case "warn"
  case "warn":
    warn(message);
    break;
  case "error":
    fail(message);
    break;
}
  • Readers expect the catch-all branch at the end, so a default placed earlier is easy to miss when scanning for the clause that handles a given value.

  • It is a live hazard rather than a style preference: a default that does not end in break, return or throw falls through into whichever clause happens to follow it.

  • Reordering an unrelated case then changes what the fall-through runs, so the bug appears in a commit that never touched the default.

Remediation

Move the default clause to the end of the switch, and give it an explicit break or return.

// Good
switch (level) {
  case "warn":
    warn(message);
    break;
  case "error":
    fail(message);
    break;
  default:
    log("unknown level");
    break;
}

© 2026 Xygeni. All rights reserved