01 / 20

Switch Statements
in JavaScript

Master the switch statement to write cleaner multi-branch logic and understand fall-through behavior.

JavaScript

What Is a Switch Statement?

A switch statement evaluates a single expression once, then compares its result against a list of possible case values. When a match is found, the code block for that case runs.

It is an alternative to long chains of if...else if...else when you are comparing the same variable against many discrete values.

Anatomy of the Switch

switch (expression) {
case value1: // code to run break; case value2: // code to run break; default: // fallback code }
expression
Evaluated once at the top
case
Compared with strict equality (===)
break
Exits the switch block
default
Runs when no case matches

Why Use Switch Over If-Else?

1
Readability: When comparing the same variable against 3+ values, switch is easier to scan than nested if-else chains.
2
Performance: Engines can optimize switch into a jump table, making lookups faster for many cases.
3
Fall-through: You can intentionally group cases that share the same code, reducing duplication.

How the Switch Flows

switch(expr) case A? yes Run A + break no case B? yes Run B + break no case C? yes Run C + break no default

Switch Uses Strict Equality (===)

Each case comparison uses ===, which checks both value and type. This means the number 1 does not match the string "1".

let x = "1";

switch (x) {
case 1: console.log("number"); break; case "1": console.log("string"); // ✓ This runs break; }

Switch vs If-Else

switch if-else
Comparison type Strict equality (===) Any boolean expression
Best for Single variable, many values Ranges, complex conditions
Fall-through Yes (intentional) No
Readability at 5+ branches High Low
Can test ranges (e.g., \( x \gt 10 \))? Not directly Yes

Understanding Fall-Through

When a matching case is found and there is no break, execution "falls through" to the next case's code, regardless of whether that case matches.

case 1 no break! case 2 no break! case 3 break; case 1 break; ✓ Fall-through Clean exit

Intentional Fall-Through

Fall-through is useful when multiple cases share the same action:

let day = "Saturday";

switch (day) {
case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday": console.log("Weekday"); break; case "Saturday": case "Sunday": console.log("Weekend"); // ✓ This runs break; }

The default Clause

The default clause acts as a catch-all. It runs when no case value matches the expression. It is similar to the final else in an if-else chain.

let fruit = "Mango";

switch (fruit) {
case "Apple": console.log("$1.50"); break; case "Banana": console.log("$0.75"); break; default: console.log("Price unknown"); // ✓ Runs }

How to Write a Switch

1
Identify the expression: What single variable or value are you testing?
2
List all possible values: Write a case for each distinct value you expect.
3
Write the action: Add the code block under each case.
4
Add break statements: Place break; at the end of each case unless you intentionally want fall-through.
5
Add a default: Handle unexpected values with a default clause at the bottom.

Example 1: Grade Converter

let grade = "B";
let message;

switch (grade) {
case "A": message = "Excellent"; break; case "B": message = "Good"; // ✓ Match! break; case "C": message = "Fair"; break; default: message = "Unknown grade"; }

console.log(message); // "Good"

Example 2: Season Finder

let month = 3;
let season;

switch (month) {
case 12: case 1: case 2: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; // ✓ Match! break; case 6: case 7: case 8: season = "Summer"; break; case 9: case 10: case 11: season = "Autumn"; break; default: season = "Invalid month"; }

Example 3: switch(true) Pattern

You can pass true as the expression and use boolean conditions in each case to mimic range checks:

let score = 85;

switch (true) {
case (score >= 90): console.log("A"); break; case (score >= 80): console.log("B"); // ✓ Runs break; case (score >= 70): console.log("C"); break; default: console.log("F"); }

Common Mistakes

✕ Forgetting break
Without break, execution falls through to subsequent cases, producing unexpected output.
✕ Type mismatch
Switch uses ===. Comparing a number expression against string case values will never match.
✕ No default clause
Omitting default means unhandled values pass silently. Always include a fallback.

Question 1 of 5

What type of comparison does a JavaScript switch statement use to match case values?

Question 2 of 5

What is logged to the console?

Question 3 of 5

Which keyword is used to define the catch-all branch in a switch statement?

Question 4 of 5

What is logged to the console?

Question 5 of 5

When is intentional fall-through most useful in a switch statement?