Control flow · Lesson 10 of 45
Conditions: if and else
Let your code make decisions.
8 minutes3 games
if runs its block when the condition is true, else when it isn't, and else if adds more conditions. They're checked top to bottom and only the first true one runs.
Step by step1 / 4
- ▶const grade = 76
- 2if (grade >= 85) {
- 3 console.log("Excellent")
- 4} else if (grade >= 65) {
- 5 console.log("Good")
- 6} else {
- 7 console.log("Keep going")
- 8}
Variables
| grade | 76 |
Output
…
JavaScript
const grade = 76
if (grade >= 85) {
console.log("Excellent")
} else if (grade >= 65) {
console.log("Good")
} else {
console.log("Keep going")
}Change the grade and see the result.
Order the lines
Order it: above 30 print Hot, otherwise Nice.
- Click the lines in the right order
What's the output?
const x = 10
if (x > 5) {
console.log("A")
} else if (x > 8) {
console.log("B")
}What will this code print?
Exercise
Print Adult if age is 18 or more, otherwise Minor.
Expected output: Adult
JavaScript
const age = 20
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.