What is Else If?
Else if lets you check multiple conditions in sequence. It's perfect when you have more than two possible outcomes!
Basic Syntax
⚡ javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (condition1) {
// Runs if condition1 is true
} else if (condition2) {
// Runs if condition1 is false AND condition2 is true
} else if (condition3) {
// Runs if condition1 and condition2 are false AND condition3 is true
} else {
// Runs if all conditions are false
}
// Example:
const score = 85
if (score >= 90) {
console.log('Grade: A')
} else if (score >= 80) {
console.log('Grade: B') // This runs
} else if (score >= 70) {
console.log('Grade: C')
} else {
console.log('Grade: F')
}How it works:
- Checks conditions from top to bottom
- Stops at the first true condition
- Exactly ONE block runs (or none if no else)
- Else is optional but recommended