TExcelTAD
Home › JavaScript › JS Switch

JavaScript Switch

A switch statement compares one value against a list of possible cases and runs the code for the matching one. It's a tidy alternative to a long chain of if…else if when every branch tests the same variable for equality.

Syntax

js · try it yourself
const day = 3;
let name;

switch (day) {
  case 1:
    name = "Monday";
    break;
  case 2:
    name = "Tuesday";
    break;
  case 3:
    name = "Wednesday";
    break;
  default:
    name = "Some other day";
}

console.log(name);
Wednesday

How it works:

  1. The expression in switch (…) is evaluated once.
  2. Its value is compared with each case using strict equality (===).
  3. Execution starts at the first matching case and continues until a break (or the end of the switch).
  4. If no case matches, the default block runs (if there is one).

Don't forget break

Without break, execution "falls through" into the next case — even though its value doesn't match:

js · try it yourself
const level = 2;
switch (level) {
  case 1:
    console.log("level 1");
  case 2:
    console.log("level 2");
  case 3:
    console.log("level 3");
  default:
    console.log("default");
}
level 2
level 3
default

This is usually a bug. Occasionally, though, fall-through is useful on purpose.

Grouping cases

Stack several case labels to share one block of code:

js · try it yourself
function typeOfDay(day) {
  switch (day.toLowerCase()) {
    case "saturday":
    case "sunday":
      return "weekend";
    case "monday":
    case "tuesday":
    case "wednesday":
    case "thursday":
    case "friday":
      return "weekday";
    default:
      return "not a day";
  }
}

console.log(typeOfDay("Sunday"), typeOfDay("FRIDAY"), typeOfDay("Funday"));
weekend weekday not a day

Inside a function, return leaves the switch (and the function) immediately, so no break is needed.

Strict comparison

Because switch uses ===, types must match. A string "1" will not match case 1:

js · try it yourself
const input = "1";   // e.g. read from a form field
switch (input) {
  case 1:
    console.log("number one");
    break;
  case "1":
    console.log("string one");
    break;
}

switch (Number(input)) {
  case 1:
    console.log("converted first, now it matches");
    break;
}
string one
converted first, now it matches

Ranges with switch (true)

switch only checks equality, but a common trick is switch (true) with conditions as cases — the first case that evaluates to true wins:

js · try it yourself
const bmi = 23.4;
switch (true) {
  case bmi < 18.5:
    console.log("underweight");
    break;
  case bmi < 25:
    console.log("normal");
    break;
  default:
    console.log("overweight");
}
normal

For ranges, an if…else if chain is usually clearer — use whichever reads better.

Alternative: lookup objects

When each case just maps a value to another value, an object (or Map) is often shorter than a switch:

js
const statusText = { 200: "OK", 404: "Not Found", 500: "Server Error" };
const text = statusText[code] ?? "Unknown";

Block scope inside cases

All cases share one block, so declaring the same let/const name in two cases is an error. Wrap a case in braces to give it its own scope:

js
switch (action) {
  case "add": {
    const message = "Added";
    break;
  }
  case "remove": {
    const message = "Removed";   // fine — different block
    break;
  }
}

Test yourself

What comparison does switch use to match cases?

  1. ==
  2. ===
  3. Object.is
  4. <

What happens when a matching case has no break?

  1. A SyntaxError
  2. The switch ends anyway
  3. Execution falls through into the next case
  4. The default block runs instead

When does the default block run?

  1. Always, at the end
  2. When no case matches
  3. Only when it is written first
  4. When a case has no break