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

  1. Write an if that logs a message when true.
  2. Add an else branch.
  3. Add an else if for 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.