LESSON 11
If Statements
Learning objective: Run code conditionally with if, else if, and else.
Understand
if statements choose which code to run.
An if runs its block when a condition is true. Add else if for more cases and else for a fallback. Keep conditions clear and ordered from most to least specific.
Analogy: An if/else is like a bouncer: meet the condition and you are in, otherwise you take another path.
See It in Action
Grading with if/else:
let score = 82;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else {
console.log("C");
}How it works: JavaScript checks each condition in order and runs the first matching block — here it prints 'B'.
Try It Yourself
- Write an
ifthat logs a message when true. - Add an
elsebranch. - Add an
else iffor a middle case.
Quick Quiz
What runs if no if or else if condition is true?
Challenge
Write a grader that prints a letter grade from a number score.
Success condition: Your grader prints the correct grade for several test scores.
+10 XP
Lesson Complete
You answered correctly and completed the required practice. Your progress has been saved on this device.
Continue to next lesson →