Duplicate Else-If Conditions
ID |
javascript.duplicate_else_if_conditions |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
dead-code, reliability, suspicious-comparison |
Description
Reports if / else if / … chains in which two branches share the same condition. The second occurrence is unreachable — anything matching the duplicated condition fires the first branch — and almost always a copy-paste error.
Rationale
Duplicate conditions are dead code that masks a bug:
-
The developer probably intended to change the operator or comparison value in one of the branches.
-
Tests typically don’t catch the duplicate because the data falls into the first branch as designed.
-
The bug stays latent until a different code path or a refactor exposes it.
// Bad - second branch is unreachable
if (x === 1) {
return "one";
} else if (x === 2) {
return "two";
} else if (x === 1) {
return "unreachable";
}
Remediation
Either fix the duplicated condition (likely the second was meant to be a different value) or remove it.
// Good
if (x === 1) {
return "one";
} else if (x === 2) {
return "two";
} else if (x === 3) {
return "three";
}