JavaScript switch Statement

In the previous lesson, we learned how to make decisions with if/else if/else. Now let’s meet the switch statement, a cleaner way to compare one value against many possible cases.

πŸ”€ What is a switch Statement?

A switch statement checks a single value and runs the block of code that matches it. When you find yourself writing a long chain of if/else if that all compares the same variable, switch says the same thing with less clutter.

Here is the basic shape:

switch.js
switch (value) {
case x:
// runs when value === x
break;
case y:
// runs when value === y
break;
default:
// runs when nothing matches
}

Let’s walk through the shape line by line:

  • switch (value) takes the value we want to compare and hands it to the cases below.
  • case x: runs its block when value === x, and case y: does the same for y.
  • break; stops the switch once a matching case finishes.
  • default: runs when none of the cases match.

JavaScript takes the value at the top, walks down the cases, and runs the first one that matches.

🧩 The Parts of a switch

Each piece of a switch has a job. Let’s break them down.

Part What it does
switch (value) The value we want to compare
case x: A value to check value against
break Stops the switch so it does not keep running
default Runs when no case matches

πŸ“… A Simple Example

Let’s turn a day number into a day name. We compare the day variable against each case until one matches.

switch.js
const day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday"); // βœ… this matches
break;
default:
console.log("Some other day");
}

Here is how JavaScript runs this code:

  • const day = 3; sets the value we are about to compare.
  • switch (day) starts checking day against each case from the top.
  • case 1 and case 2 do not match, so JavaScript skips them.
  • case 3 matches, so it prints "Wednesday" and then hits break to stop.
  • default never runs here because a case already matched.

πŸ›‘ The Role of break

The break keyword tells JavaScript to leave the switch once a matching case is done. Without it, JavaScript keeps running the next cases too, even if they do not match. This is called fall-through.

Watch what happens when we forget break:

switch.js
const day = 1;
switch (day) {
case 1:
console.log("Monday"); // matches, but no break
case 2:
console.log("Tuesday"); // ❌ this also runs!
case 3:
console.log("Wednesday"); // ❌ and so does this!
}

Let’s trace why all three lines print:

  • case 1 matches, so it prints "Monday".
  • With no break, JavaScript falls into case 2 and prints "Tuesday" even though day is not 2.
  • It keeps falling through into case 3 and prints "Wednesday" too.

Almost always you want a break at the end of each case.

Always add break

Forgetting break is the most common switch bug. Unless you have a good reason to let cases fall through, end every case with break.

🎯 The default Case

The default case runs when none of the cases match. Think of it as the else of a switch. It is optional, but adding it means your code always has an answer.

switch.js
const color = "purple";
switch (color) {
case "red":
console.log("Stop");
break;
case "green":
console.log("Go");
break;
default:
console.log("Unknown color"); // βœ… runs because nothing matched
}

Here is what happens step by step:

  • const color = "purple"; sets the value we compare.
  • case "red" and case "green" do not match, so JavaScript skips both.
  • default runs because nothing matched, printing "Unknown color".

πŸ” switch Uses Strict Comparison

A switch compares each case using strict equality (===), so the value and the type must both match. There is no type conversion here.

switch.js
const input = "5";
switch (input) {
case 5:
console.log("Number five");
break;
case "5":
console.log("String five"); // βœ… this matches
break;
}

Let’s see why only one case matches:

  • const input = "5"; stores the string "5", not the number 5.
  • case 5 does not match because the number 5 is a different type from the string "5".
  • case "5" matches exactly, so it prints "String five".

The number and the string are different values to a switch.

No loose comparison

Unlike ==, a switch never converts types for you. "5" and 5 are not the same case.

πŸ” A Practical Example

A switch shines when a single choice triggers different actions. Let’s handle a menu choice where each option does something different.

switch.js
const choice = "save";
switch (choice) {
case "new":
console.log("Creating a new file...");
break;
case "open":
console.log("Opening a file...");
break;
case "save":
console.log("Saving your work..."); // βœ… this runs
break;
default:
console.log("Command not recognized");
}

Here is how the menu picks an action:

  • const choice = "save"; holds the command we want to handle.
  • case "new" and case "open" do not match, so JavaScript skips them.
  • case "save" matches, so it prints "Saving your work..." and then break stops the switch.
  • default catches any command we did not plan for, like a typo.

πŸ€” switch vs if/else if

Both tools make decisions, so which one fits? Use a switch when you are checking one value against many fixed options. Reach for if/else if when your conditions are ranges or involve different variables.

Use a switch when… Use if/else if when…
You compare one value against many fixed options You test ranges like score > 90
The options are exact, known values The conditions use different variables
You want clean, readable branches You need complex logic with && or ||

⚠️ Common Mistakes to Avoid

Mistake Problem Solution
Forgetting break Code falls through and runs the next cases too End each case with break
No default case Nothing runs when no case matches Add a default to handle the leftover values
Expecting loose comparison "5" does not match case 5 Make sure the value and type both match

πŸ”§ Try It Yourself!

  1. Create a switch that turns a day number (1 to 7) into a day name.
  2. Add a default case that prints a friendly message for any other number.
  3. Remove a break and watch the cases fall through, then add it back.
  4. Try matching the string "5" against case 5 to see why strict comparison matters.

🧩 What You’ve Learned

  • βœ… A switch compares one value against many cases cleanly
  • βœ… break stops the switch and prevents fall-through
  • βœ… default runs when no case matches
  • βœ… A switch uses strict comparison (===), so type matters
  • βœ… Use switch for many fixed values and if/else if for ranges or complex logic

πŸš€ What’s Next?

Now that you can branch with switch, let’s learn a shortcut for quick decisions. Let’s continue to Ternary Operator.

Share & Connect