Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript switch Statement

The switch statement is a clean way to choose between many fixed outcomes.

It compares a single value against multiple cases and runs the first match.

Why We Need It

Long else-if chains can be hard to read when every branch checks the same variable.

Switch keeps that pattern compact and easy to scan.

Syntax

switch (value) { case x: ...; break; default: ... }

Basic Example

1. Basic switch

const role = "editor";

switch (role) {
  case "admin":
    console.log("All access");
    break;
  case "editor":
    console.log("Write access");
    break;
  default:
...

Switch matches the value to one of the cases.

Real World Example

2. Grouped Cases

const day = "sat";

switch (day) {
  case "sat":
  case "sun":
    console.log("Weekend");
    break;
  default:
    console.log("Weekday");
}

Group cases when they share the same behavior.

Multiple Use Cases

Discrete Choices

Use switch when you compare one value against many fixed cases.

It can be cleaner than a long else-if ladder when all checks are equality checks.

Fall-Through

If you omit `break`, execution continues into the next case. This is called fall-through.

Sometimes it is useful, but it is a common source of bugs.

Default Case

The default block runs when no case matches.

Always include a default to handle unexpected values.

More Examples

3. Number Cases

const status = 404;

switch (status) {
  case 200:
    console.log("OK");
    break;
  case 404:
    console.log("Not Found");
    break;
  default:
...

Switch works with numbers, strings, and more.

4. Fall-Through Example

const size = "M";
let price = 0;

switch (size) {
  case "L":
    price += 2;
  case "M":
    price += 1;
  case "S":
    price += 0;
...

Without breaks, cases fall through. Use carefully.

Comparison

Without

if (role === "admin") {
  console.log("All access");
} else if (role === "editor") {
  console.log("Write access");
} else {
  console.log("Read access");
}

With

switch (role) {
  case "admin":
    console.log("All access");
    break;
  case "editor":
    console.log("Write access");
    break;
  default:
    console.log("Read access");
}

Common Mistakes and Fixes

Missing break

Add break to avoid unexpected fall-through.

No default case

Always include default for unexpected values.

Using switch for complex conditions

Switch is best for equality checks on one value.

Forgetting strict comparison

Switch uses strict equality (===), so types must match.

Interview Questions

What does switch compare with?

It uses strict equality (===) against each case.

What is fall-through?

When one case continues into the next because break is missing.

When is switch a good choice?

When comparing one value to many fixed cases.

Practice Problem

Practice: Use switch to map a plan name to a monthly price.

const plan = "pro";
// TODO: log price for basic, pro, or enterprise

One Possible Solution

const plan = "pro";
switch (plan) {
  case "basic":
    console.log(10);
    break;
  case "pro":
    console.log(25);
    break;
  case "enterprise":
    console.log(50);
    break;
  default:
    console.log(0);
}

Frequently Asked Questions

Does switch use strict equality?

Yes, switch compares using ===.

Can I use expressions in case?

Yes, but they are matched against the switch value.

Is fall-through always bad?

No, it can be useful for grouped cases, but use it intentionally.

Try It Yourself

Try different values to see how switch selects a case.