What is the Else Statement?
The else statement lets you run code when the if condition is false. It handles the "otherwise" case!
Basic Syntax
⚡ javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (condition) {
// Runs if condition is true
} else {
// Runs if condition is false
}
// Example:
const age = 15
if (age >= 18) {
console.log('You can vote!')
} else {
console.log('Too young to vote') // This runs
}Think of it like:
- IF it's raining, bring an umbrella
- ELSE (otherwise), wear sunglasses