How to Use the JavaScript Switch Case: Full Guide

The switch statement compares one value against multiple cases. Learn the syntax, how break and default work, and when switch is cleaner than if/else.

5 min read

A switch statement compares one value against several possible cases and runs the matching code. It is an alternative to a long if/else if chain when you are checking the same variable against different exact values.

javascriptjavascript
const fruit = "apple";
switch (fruit) {
  case "banana": console.log("Yellow and curved."); break;
  case "apple":  console.log("Red or green, crisp."); break;
  case "orange": console.log("Citrus and round."); break;
  default:       console.log("Unknown fruit.");
}
// "Red or green, crisp." prints because fruit is "apple"

This is the switch pattern: one value compared against multiple cases, with a break after each and a default at the end.

The switch Syntax

Stripped down to its skeleton, a switch statement is just a value, a list of possible matches, and an optional fallback. Here is the general shape without any real logic filled in:

javascriptjavascript
switch (expression) {
  case value1:
    // code for value1
    break;
  case value2:
    // code for value2
    break;
  default:
    // code when nothing matches
}

Every switch statement has four parts:

  • switch (expression): the value to compare. This is evaluated once at the start.
  • case value:: a possible match. JavaScript compares the expression against this value using strict equality (===).
  • break: exits the switch. Without it, execution "falls through" to the next case.
  • default: runs when no case matches. It is optional but recommended, like else at the end of an if chain.

How switch Evaluates

switch statement evaluation flow

JavaScript checks each case from top to bottom. The first match that is strictly equal to the expression runs. After the matching code executes, break exits the entire switch. If no case matches, default runs.

The break Keyword

break stops execution and jumps out of the switch. Without it, JavaScript keeps running the next case's code even if that case does not match:

javascriptjavascript
const day = "Monday";
switch (day) {
  case "Monday":
    console.log("Start of week.");
    // Missing break!
  case "Tuesday": console.log("Second day."); break;
  default:        console.log("Other day.");
}
// "Start of week."
// "Second day."   -- also prints because there is no break after Monday

The missing break after "Monday" causes fall-through: both Monday's and Tuesday's code run. This is almost always a bug. Always add break after each case.

Intentional Fall-Through

Sometimes fall-through is useful. When multiple cases should run the same code, stack them without a break:

javascriptjavascript
const day = "Saturday";
switch (day) {
  case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday":
    console.log("Weekday. Time to work.");
    break;
  case "Saturday": case "Sunday":
    console.log("Weekend. Time to relax.");
    break;
}
// "Weekend. Time to relax."

The five weekday cases all fall through to the same log statement. The two weekend cases do the same. This is cleaner than repeating the same code six times or writing a complex condition.

The default Case

default runs when no case matches. Think of it as the else at the end of an if/else chain:

javascriptjavascript
const status = "unknown";
switch (status) {
  case "active":   console.log("User is active."); break;
  case "inactive": console.log("User is inactive."); break;
  default:         console.log("Unknown status:", status);
}
// "Unknown status: unknown"

Always include a default case, even if you think every possible value is covered. It catches unexpected values and makes debugging easier. You can place default anywhere in the switch, but putting it last is the clearest convention.

switch Uses Strict Equality

switch compares using strict equality (===), so the type must match:

javascriptjavascript
const value = "3";
switch (value) {
  case 3:   console.log("Number 3"); break;
  case "3": console.log("String 3"); break;
}
// "String 3" -- "3" === "3" is true, "3" === 3 is false

This is different from if with loose equality, which would match both. For more on this distinction, see the comparison operators guide.

When switch Shines

Use switch when you are comparing one variable against many specific values. This if/else if version repeats the same variable name in every branch:

javascriptjavascript
// if/else if -- repetitive variable name
if (direction === "up") { moveUp(); }
else if (direction === "down") { moveDown(); }
else if (direction === "left") { moveLeft(); }
else if (direction === "right") { moveRight(); }
else { console.log("Unknown direction"); }

The equivalent switch statement writes direction once and lists every value it might match, instead of repeating the same comparison in front of each branch:

javascriptjavascript
// switch -- variable name appears once
switch (direction) {
  case "up":    moveUp(); break;
  case "down":  moveDown(); break;
  case "left":  moveLeft(); break;
  case "right": moveRight(); break;
  default:      console.log("Unknown direction");
}

The switch version reads as "check direction against up, down, left, right." The if/else version repeats the direction comparison four times. When every condition is a simple equality check against the same variable, switch is usually cleaner. For a deeper comparison, see the switch vs if else guide.

Rune AI

Rune AI

Key Insights

  • switch compares one expression against multiple case values using strict equality (===).
  • Each case should end with break to prevent unintentional fall-through.
  • The default case runs when no other case matches, like else in an if chain.
  • Fall-through can be intentional: empty cases share the next case's code.
  • Use switch for exact value matches; use if/else if for ranges and complex conditions.
  • switch works with any data type: strings, numbers, booleans, and more.
RunePowered by Rune AI

Frequently Asked Questions

What happens if I forget break in a switch case?

Without break, JavaScript falls through to the next case and executes its code too. This continues until a break is hit or the switch ends. Sometimes this is intentional, but it is usually a bug. Always add break unless you specifically want fall-through behavior.

Can I use ranges or conditions inside a case?

No. Each case compares against the switch value using strict equality (===). You cannot write case > 10: or case age < 18:. For ranges and conditions, use an if/else if chain instead.

Does switch work with strings or only numbers?

switch works with any value: strings, numbers, booleans, and even objects. The comparison uses === so the type must match exactly. '3' does not match the number 3.

Conclusion

The switch statement is a clean way to compare one value against many specific cases. Use it when you find yourself writing the same variable name repeatedly in an if/else if chain. Remember to add break after each case unless you want intentional fall-through, always include a default case as a safety net, and know that switch uses strict equality (===) for comparisons.