else if and else
More choices.
Often there are more than two possibilities. A grade could be an A, a B, a C, or a fail. Else if adds more conditions, and else catches everything that's left.

- More than two choices
- else if adds conditions
- else catches the rest
A grade
JavaScript checks each condition from the top, in order. The first one that's true runs, and the rest are skipped. If none are true, the else block runs. Run it. Seventy eight isn't ninety or more, and it isn't eighty or more, but it is seventy or more. So it's a C.
const score = 78;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("Keep practising");
}C
Order matters
Because the first true condition wins, put the most specific checks first. If we checked score greater than or equal to seventy first, a score of ninety five would get a C. Always test the boundaries, like exactly eighty, and exactly ninety.
| key | value |
|---|---|
| if | first check |
| else if | next checks, in order |
| else | nothing else matched |
| First true | wins |
Summary
Else if adds more conditions, checked in order, and else handles everything else. Next, combining conditions with and, or, and not.
Key takeaways- if / else if / else
- First true wins
- Test the boundaries