Lesson 4: The switch Statement

Learning Objectives

By the end of this lesson, learners will:

  • Understand what the switch statement is and why it’s used
  • Know the correct syntax and structure of switch
  • Be able to use case, break, and default correctly
  • Learn when to choose switch over if...else
  • Practice using switch for multi-branch decision-making

What Is the switch Statement?

A switch statement is a control flow structure that allows you to compare one value against many possible cases.

It is a cleaner and more readable alternative to writing multiple if...else if statements.


Why Use switch?

Let’s say you’re building an app that gives the user a message based on the day of the week. You could use multiple if statements:

if (day === "Monday") {
  // ...
} else if (day === "Tuesday") {
  // ...
} else if (day === "Wednesday") {
  // ...

That works — but a switch statement makes it much easier to read and maintain.


Basic switch Syntax

switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // code block
}
  • The expression is evaluated once.
  • The value is then compared to each case.
  • If a match is found, the corresponding block runs.
  • The break statement prevents “fall-through.”
  • The default block runs if no cases match.

Example: Days of the Week

let day = "Wednesday";

switch (day) {
  case "Monday":
    console.log("Start of the week!");
    break;
  case "Wednesday":
    console.log("Midweek check-in!");
    break;
  case "Friday":
    console.log("Almost weekend!");
    break;
  default:
    console.log("Have a nice day!");
}

Output:

Midweek check-in!

The break Keyword

Each case should end with a break unless you want to run the next case (which is rare for beginners).

Without break, JavaScript will fall through and run all remaining cases.

Example:

let mood = "happy";

switch (mood) {
  case "happy":
    console.log("Smile!");
  case "sad":
    console.log("Cheer up!");
}

Output:

Smile!
Cheer up!

🛑 That’s probably not what you intended. Add break to fix it:

case "happy":
  console.log("Smile!");
  break;

Using the default Case

The default block is like an else — it runs if none of the case values match.

Example:

let fruit = "pear";

switch (fruit) {
  case "apple":
    console.log("Apples are sweet.");
    break;
  case "banana":
    console.log("Bananas are yellow.");
    break;
  default:
    console.log("Unknown fruit.");
}

Output:

Unknown fruit.

Real-Life Use Case: Traffic Light

let light = "green";

switch (light) {
  case "red":
    console.log("Stop!");
    break;
  case "yellow":
    console.log("Slow down.");
    break;
  case "green":
    console.log("Go!");
    break;
  default:
    console.log("Invalid color.");
}

Output: Go!


When Should I Use switch?

Use switch when:

  • You’re comparing one value against many specific options
  • You want a cleaner alternative to long if...else if...else chains

Use if when:

  • You need to check multiple unrelated conditions
  • You’re testing range-based or complex logic

Summary Table

KeywordDescription
switchEvaluates one value
caseA possible match value
breakExits the switch block after a match
defaultRuns if no case matches