JavaScript switch Statement
Table of Contents + β
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 (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 whenvalue === x, andcase y:does the same fory.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.
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 checkingdayagainst each case from the top.case 1andcase 2do not match, so JavaScript skips them.case 3matches, so it prints"Wednesday"and then hitsbreakto stop.defaultnever 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:
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 1matches, so it prints"Monday".- With no
break, JavaScript falls intocase 2and prints"Tuesday"even thoughdayis not2. - It keeps falling through into
case 3and 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.
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"andcase "green"do not match, so JavaScript skips both.defaultruns 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.
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 number5.case 5does not match because the number5is 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.
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"andcase "open"do not match, so JavaScript skips them.case "save"matches, so it prints"Saving your work..."and thenbreakstops the switch.defaultcatches 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!
- Create a
switchthat turns a day number (1to7) into a day name. - Add a
defaultcase that prints a friendly message for any other number. - Remove a
breakand watch the cases fall through, then add it back. - Try matching the string
"5"againstcase 5to see why strict comparison matters.
π§© What Youβve Learned
- β
A
switchcompares one value against many cases cleanly - β
breakstops the switch and prevents fall-through - β
defaultruns when no case matches - β
A
switchuses strict comparison (===), so type matters - β
Use
switchfor many fixed values andif/else iffor 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.