JavaScript Switch

Match One Value Against Many

JavaScript Switch

`switch` picks a branch based on a value. Cleaner than a long `if/else if` chain when you have many discrete cases.

5 min read Level 1/5 #switch#case#break
What you'll learn
  • Match a value with `case`
  • Always include `break` (and know what happens when you don't)
  • Provide a `default`
  • Recognize when fall-through is intentional

switch lets you compare a value against many possibilities, one case at a time. It’s especially handy when you have many discrete options.

A basic switch script.js
const day = 3;

switch (day) {
  case 1: console.log("Monday");    break;
  case 2: console.log("Tuesday");   break;
  case 3: console.log("Wednesday"); break;
  case 4: console.log("Thursday");  break;
  case 5: console.log("Friday");    break;
  case 6: console.log("Saturday");  break;
  case 7: console.log("Sunday");    break;
  default: console.log("Not a day");
}
// "Wednesday"
▶ Preview: console

Each case Needs a break

switch falls through to the next case unless you stop it with break. Forgetting it is one of the classic switch bugs.

The fall-through trap script.js
const fruit = "apple";

switch (fruit) {
  case "apple":
    console.log("It's an apple.");
    // no break!
  case "banana":
    console.log("It's a banana.");
    break;
  case "grape":
    console.log("It's a grape.");
    break;
}
// "It's an apple."
// "It's a banana."   ← oops, fell through
▶ Preview: console

Intentional Fall-Through

Sometimes fall-through is what you want — to handle multiple values the same way:

Grouping cases script.js
const day = 6;

switch (day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    console.log("Weekday");
    break;
  case 6:
  case 7:
    console.log("Weekend");
    break;
  default:
    console.log("Not a day");
}
// "Weekend"
▶ Preview: console

default — the fallback

default runs when no case matches. It’s optional but recommended — if you don’t have one and nothing matches, the switch does nothing.

default at the bottom script.js
const role = "ghost";

switch (role) {
  case "admin":  console.log("Full access"); break;
  case "user":   console.log("Limited access"); break;
  case "guest":  console.log("Read only"); break;
  default:       console.log("Unknown role: " + role);
}
// "Unknown role: ghost"
▶ Preview: console

switch Uses Strict Equality

switch compares with ===. So case "5" won’t match the number 5.

Strict matching script.js
const value = 5;

switch (value) {
  case "5":
    console.log("string five");
    break;
  case 5:
    console.log("number five");
    break;
}
// "number five"
▶ Preview: console

if/else if or switch?

Both work — pick by clarity:

  • Use switch when you’re testing one value against many specific possibilities. Cleaner and faster to scan.
  • Use if/else if when conditions are range checks, multiple variables, or complex expressions.

For mapping a value to a result, you might also reach for a plain object lookup:

Lookup object alternative script.js
const days = {
  1: "Monday", 2: "Tuesday", 3: "Wednesday",
  4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday",
};

const day = 3;
console.log(days[day] ?? "Not a day");
// "Wednesday"
▶ Preview: console

Often the cleanest of all.

Try It Yourself

Exercise

Number to weekday

Difficulty 1/5~3 min
Given `const day = 3`, use a `switch` statement to build a constant `name` that holds the weekday name (`"Monday"` for `1`, `"Tuesday"` for `2`, …, `"Sunday"` for `7`). For unknown values, set `"Not a day"`. Log it. For `day = 3`, the expected output is `"Wednesday"`.
solution.js
const day = 3;
let name;
// your code here
2tests will run
💡 Show hint
Use a `switch (day)` with seven `case` blocks and a `default`. Don't forget `break` after each case.
✅ Show solution
const day = 3;
let name;
switch (day) {
  case 1: name = "Monday";    break;
  case 2: name = "Tuesday";   break;
  case 3: name = "Wednesday"; break;
  case 4: name = "Thursday";  break;
  case 5: name = "Friday";    break;
  case 6: name = "Saturday";  break;
  case 7: name = "Sunday";    break;
  default: name = "Not a day";
}
console.log(name);

Up Next

The logical operators that combine conditions.

JavaScript Logical Operators →