Aral Code

Decisions & Loops Lesson 13 of 30

JavaScript switch Statement

Choose between many exact values with switch, case, break and default — with a day-of-the-week schedule and a sari-sari store menu.

When you compare one value against many exact possibilities, like the day of the week or a menu choice, a long else if chain gets repetitive. The switch statement is a cleaner way to write it.

How switch works

switch (value) {
  case option1:
    // code if value === option1
    break;
  case option2:
    // code if value === option2
    break;
  default:
    // code if nothing matched
}
  • switch looks at the value once.
  • It jumps to the case that matches, using strict equality (===).
  • break ends the switch. Without it, the code keeps running into the next case.
  • default runs when no case matches, like else.
Example Today's class schedule
<p id="out"></p>
<script>
  const day = new Date().getDay();   // 0 = Sunday … 6 = Saturday
  let plan;
  switch (day) {
    case 1:
      plan = "Monday: Flag ceremony at 7:00 AM 🇵🇭";
      break;
    case 3:
      plan = "Wednesday: PE uniform day 🏃";
      break;
    case 5:
      plan = "Friday: Coding club after class 💻";
      break;
    case 0:
    case 6:
      plan = "Weekend! Walang pasok. 😄";
      break;
    default:
      plan = "Regular class day 📚";
  }
  document.getElementById("out").textContent = plan;
</script>

Grouping cases

In the example, case 0: and case 6: are stacked with no code between them. Both lead to the same code: "Weekend!". This is the clean way to say "if it's 0 or 6".

Forgetting break

Example What happens without break
<pre id="out"></pre>
<script>
  const size = "M";
  let log = "";
  switch (size) {
    case "S":
      log += "Small\n";
    case "M":
      log += "Medium\n";
    case "L":
      log += "Large\n";
    default:
      log += "Unknown size\n";
  }
  document.getElementById("out").textContent = log;
</script>

The size is "M", but the output shows Medium, Large and Unknown size. After a match, the code "falls through" to every case below until it meets a break. Add break; after each line and run it again.

A store menu

Example Order by number
<p>1 – Pandesal (₱25)<br>2 – Kape (₱8)<br>3 – Itlog (₱9)</p>
<input id="choice" type="number" min="1" max="3" value="2" style="width:60px">
<button onclick="order()">Order</button>
<p id="out"></p>
<script>
  function order() {
    const choice = Number(document.getElementById("choice").value);
    let item, price;
    switch (choice) {
      case 1: item = "Pandesal"; price = 25; break;
      case 2: item = "Kape"; price = 8; break;
      case 3: item = "Itlog"; price = 9; break;
      default: item = null;
    }
    document.getElementById("out").textContent =
      item ? `You ordered ${item}. That's ₱${price}.` : "Pumili lang po ng 1, 2 o 3.";
  }
</script>

Notice Number(...). The input's value is a string, and switch uses ===, so "2" would never match case 2. Converting first is essential.

switch or if…else?

Use switch when…Use if…else when…
Comparing one value to exact optionsChecking ranges like grade >= 90
Days, menu choices, commandsCombining conditions with && or ||
There are many optionsThere are only two or three options

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does break do inside a switch?
2. When does default run?
3. Which comparison does switch use?
4. Which situation fits if…else better than switch?

By Sir Reyel · Last updated 2026-12-04