Skip to content

noDuplicateElseIf (since v1.6.2)

Diagnostic Category: lint/nursery/noDuplicateElseIf

Sources:

Disallow duplicate conditions in if-else-if chains

if-else-if chains are commonly used when there is a need to execute only one branch (or at most one branch) out of several possible branches, based on certain conditions.

Two identical test conditions in the same chain are almost always a mistake in the code. Unless there are side effects in the expressions, a duplicate will evaluate to the same true or false value as the identical expression earlier in the chain, meaning that its branch can never execute.

Please note that this rule does not compare conditions from the chain with conditions inside statements

if (a) {
foo();
} else if (b) {
bar();
} else if (b) {
baz();
}
nursery/noDuplicateElseIf.js:5:12 lint/nursery/noDuplicateElseIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.
  
    3 │ } else if (b) {
    4 │     bar();
  > 5 │ } else if (b) {
              ^
    6 │     baz();
    7 │ }
  
if (a) {
foo();
} else if (b) {
bar();
} else if (c) {
baz();
}