12 `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.
24 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.
36 In the above example, `baz()` can never execute. Obviously, `baz()` could be executed only when `b` evaluates to `true`, but in that case `bar()` would be executed instead, since it's earlier in the chain.
40 This rule disallows duplicate conditions in the same `if-else-if` chain.
42 Examples of **incorrect** code for this rule:
47 /*eslint no-dupe-else-if: "error"*/
51 } else if (isSomething(x)) {
82 Examples of **correct** code for this rule:
87 /*eslint no-dupe-else-if: "error"*/
91 } else if (isSomethingElse(x)) {
109 } else if (n === 2) {
111 } else if (n === 3) {
113 } else if (n === 4) {
115 } else if (n === 5) {
122 This rule can also detect some cases where the conditions are not identical, but the branch can never execute due to the logic of `||` and `&&` operators.
124 Examples of additional **incorrect** code for this rule:
129 /*eslint no-dupe-else-if: "error"*/
153 } else if (a && b && c) {
167 } else if (d && (c && e && b || a)) {
174 Please note that this rule does not compare conditions from the chain with conditions inside statements, and will not warn in the cases such as follows:
192 ## When Not To Use It
194 In rare cases where you really need identical test conditions in the same chain, which necessarily means that the expressions in the chain are causing and relying on side effects, you will have to turn this rule off.