Chapter V · Control Flow
The else if Statement
else if chains multiple conditions together so they're tested in order, stopping at the first one that matches.
01 What Is else if?
else if is not a separate keyword — it's an else block whose body is itself a new if statement. Chaining several of them lets a program test conditions in sequence and pick the first branch that matches, which is how most multi-way decisions in JavaScript are written.
02 Simple Example
Grading a score against three ranges.
const grade = 75;\n\nif (grade >= 90) {\n console.log("A");\n} else if (grade >= 75) {\n console.log("B");\n} else {\n console.log("C");\n}
75 >= 90 is false, so the engine moves to the next check: 75 >= 75 is true, so it logs "B" and never evaluates the final else at all.
03 How It Works
Conditions are tested strictly top to bottom. The moment one is truthy, its block runs and every remaining condition — even ones that would also be true — is skipped entirely. Order therefore matters: putting a broader condition before a narrower one can silently shadow it.
04 Step-by-Step
Evaluate the first if condition; run its block and stop here if truthy.
Otherwise, evaluate the next else if condition in order.
Repeat for each else if in the chain.
If none matched, run the final else block (if one exists).
05 Common Mistakes & Edge Cases
Ordering bugs. if (grade >= 75) checked before if (grade >= 90) would classify a 95 as "B", because the first truthy match wins. Order ranges from most specific to least specific.
Long chains get hard to read. Beyond three or four branches on the same variable, a switch statement or a lookup object is usually clearer.
06 Mental Model
"A triage line — the first matching category wins, and no one is checked twice."
Think of patients sorted by the first rule they satisfy, most urgent first. Once sorted into a category, they don't get re-evaluated against the rest of the list.
07 Interview Questions
Q: Does else if re-check earlier conditions?
No — once a branch matches, the whole chain exits immediately; later conditions aren't evaluated at all, even if they'd also be true.
Key Takeaway
else if chains are evaluated top to bottom, and the first truthy condition wins — so branch order, from most to least specific, decides the outcome.