src/content/docs/linter/rules/no-duplicate-else-if.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.6.2` - Diagnostic Category: [`lint/suspicious/noDuplicateElseIf`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-dupe-else-if`](https://eslint.org/docs/latest/rules/no-dupe-else-if){
"linter": {
"rules": {
"suspicious": {
"noDuplicateElseIf": "error"
}
}
}
}
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();
}
if (a) {
foo();
} else if (b) {
bar();
} else if (c) {
baz();
}