switch

Understand how the switch statement compares a single value against multiple cases, why break and default matter, and when switch is a better choice than a long if/else if chain.

A switch statement is an alternative to a long else if chain, built specifically for comparing one value against many possible exact matches. It can make code that checks a single variable against many discrete options easier to read.

How switch evaluates

A switch statement takes one expression and compares it, using strict equality (===), against the value of each case label in order. When it finds a case whose value matches, execution jumps into that case's code.

Because the comparison is strict equality, switch (unlike some loosely-typed comparisons) never coerces types to make a match — the string "1" will not match case 1, and vice versa.

break: stopping the fall-through

Once a matching case is found, JavaScript does not automatically stop at the end of that case's block — it keeps executing every statement in every case below it, in order, until it hits a break statement or reaches the end of the switch. This is called fall-through.

Almost every case in a typical switch ends with a break statement so that only the matched case's code runs, and control then exits the switch entirely and continues with the code after it.

default: the catch-all case

A default case runs when none of the other case labels matched the switch expression. It behaves like the final else in an if/else if chain, and is optional but recommended so unexpected values are handled explicitly rather than silently ignored.

default does not have to be the last case written (JavaScript will still find it wherever it appears), but placing it last is a strong, almost universal convention because it reads naturally as "everything else falls here."

Intentional fall-through

Fall-through is usually treated as a bug to avoid with break, but it can also be used intentionally: stacking multiple case labels with no code (and no break) between them lets several different values share one block of code.

This pattern is common when grouping related values — for example, treating both "Sat" and "Sun" as a weekend using two case labels back to back before the shared code block.

switch vs if/else if

switch shines when you are checking one variable against many specific, discrete values — it reads more cleanly than a long chain of === comparisons. if/else if is more flexible because each condition can be a completely different kind of expression (ranges, combined conditions with && and ||, function calls), which switch cannot do naturally.

switch statement structure

Comparisons use strict equality (===) — no type coercion happens. Without break, execution falls through into the next case's code. default is optional and can appear anywhere, though it is conventionally placed last.

switch (expression) {
  case value1:
    // code for value1
    break;
  case value2:
    // code for value2
    break;
  default:
    // code if nothing matched
}

Code Examples

JavaScript compares day against each case in order. "Tue" matches the second case, so that block runs, and break stops execution right after, skipping "Fri" and default entirely. Expected result: Second day case "Tue": matches exactly because switch uses strict equality — no coercion is attempted.

case "Sat" has no code of its own, so it falls straight through into case "Sun"'s return true — this is intentional fall-through used to group two values that should behave identically. Using return inside a function also means an explicit break is unnecessary, since return exits the whole function immediately. Expected result: true false case "Sat": (empty) falls through to case "Sun": return true; — both weekend days share one result.

Common mistake: Forgetting the break (or a return) after a case with actual code causes it to silently run every case below it too — a classic switch bug.

const day = "Tue";

switch (day) {
  case "Mon":
    console.log("Start of the work week");
    break;
  case "Tue":
    console.log("Second day");
    break;
  case "Fri":
    console.log("Almost the weekend");
    break;
  default:
    console.log("Just another day");
}
function isWeekend(day) {
  switch (day) {
    case "Sat":
    case "Sun":
      return true;
    case "Mon":
    case "Tue":
    case "Wed":
    case "Thu":
    case "Fri":
      return false;
    default:
      throw new Error("Invalid day: " + day);
  }
}

console.log(isWeekend("Sat"));
console.log(isWeekend("Wed"));

switch fall-through without break

Without a break statement, matching one case does not stop the switch — execution continues into every case below it until a break is reached.

switch (value) {
  case "a":
    doA();      // no break!
  case "b":
    doB();      ← execution FALLS INTO here too
    break;
  case "c":
    doC();
}

value === "a"  →  runs doA() AND doB(), then stops at break

Important Rules

switch compares the expression to each case using strict equality (===); no type coercion occurs.

Without a break (or return/throw), execution falls through into the next case's code, regardless of whether that case's own value matches.

default runs when no case matched, and does not need to be the last case, though convention places it last.

Stacking case labels with no code between them is a valid and common way to group values that share behavior.

Inside a function, return can substitute for break since it exits immediately.

Where this shows up

switch is common when handling a fixed, known set of outcomes: routing an HTTP status code to a message, mapping a Redux/reducer action type to state changes, or converting a numeric day-of-week into its name.

case "1": // when the value is the number 1 case 1: // matches the number 1 exactly

Because switch uses strict equality, the string "1" and the number 1 are never equal, so case "1" will never match a switch(1) — make sure the case label's type matches the switch expression's type.

switch (status) { case "active": console.log("Active user"); case "inactive": console.log("Inactive user"); break; } switch (status) { case "active": console.log("Active user"); break; case "inactive": console.log("Inactive user"); break; }

Missing the break after the "active" case means that when status is "active", both messages print — an unintended fall-through bug.

[Multiple Choice] What happens if a matched case block has no break statement and there is a case below it? A. JavaScript throws a syntax error B. Execution falls through and runs the code in the following case(s) too C. The switch simply exits without running anything else D. The default case runs instead

B. Execution falls through and runs the code in the following case(s) too — Without break, execution keeps running downward through every subsequent case's code until it hits a break or the end of the switch — this is called fall-through.

Write a function httpStatusMessage(code) using switch that returns "OK" for 200, "Not Found" for 404, "Server Error" for 500, and "Unknown Status" for anything else.

function httpStatusMessage(code) { switch (code) { case 200: return "OK"; case 404: return "Not Found"; case 500: return "Server Error"; default: return "Unknown Status"; } }

Remember to add break after each case, and put the default case last.