Skip to content
Theory · Conditions and loops · lesson 2 of 13

switch

The lobby of a large building has a directory on the wall: "Accounting, floor 3; HR, floor 5; cafeteria, floor 1." You don't walk through every floor in turn. You find your line in the directory and go straight to the right floor.

An else if chain is like walking through the floors. When one variable is compared with several values, the chain repeats the same comparison in every branch. The switch statement works like the lobby directory: it evaluates the expression in parentheses once and jumps to the case with the same value:

JavaScript
const command = "stop";

switch (command) {
    case "start":
        console.log("Starting");
        break;
    case "stop":
        console.log("Stopping"); // Stopping
        break;
    default:
        console.log("Unknown command");
}

Let's go through the program:

  1. switch (command) evaluates command and gets "stop".
  2. case "start": does not match, so this branch is skipped.
  3. case "stop": matches, so the code under it runs, and the program prints Stopping.
  4. break ends the switch, and default is never reached.

The case and default branches

A switch has two kinds of branches, and each has its own role.

The case branch

The word case is followed by a value and a colon, and the branch's code goes below. Execution starts at the line after the matching case.

There can be any number of case branches, and they are checked in turn, from top to bottom. The first match decides where execution starts.

The default branch

The default branch runs when no case matches. It usually goes last, like else in an if chain.

If no case matches and there is no default branch, nothing runs. The program moves on past the closing brace of the switch.

Where a branch ends

A case branch does not end by itself. Execution goes down until something stops it.

With break

The break statement ends the whole switch, and the program continues after its closing brace. That is why break goes at the end of every branch.

With return

Inside a function, a branch often ends with return. It leaves the whole function, so no break is needed after it.

Falling through

Without break, execution does not stop at the next case but falls through into its code without checking its value. This is used when several values share code: the empty case 6: falls through into case 7:.

JavaScript
const day = 6;

switch (day) {
    case 6:
    case 7:
        console.log("weekend"); // weekend
        break;
    default:
        console.log("workday");
}

Let's see what happens:

  1. day is 6, so execution starts under case 6:.
  2. There is no code under it, and execution falls through into case 7: without comparing 6 with 7.
  3. console.log("weekend") prints weekend, and break ends the switch.

When day is 6 or 7, the program prints weekend; for any other value, it prints workday.

Here the directory analogy stops working. In a directory every line stands apart, while in a switch the branches follow one another, and without break execution flows into the next one.

Comparison with ===

switch compares the value with each case using strict equality, ===, with no type conversion. Interviewers often ask about this: the string "2" is not equal to the number 2, so default runs here:

JavaScript
const input = "2";

switch (input) {
    case 2:
        console.log("two");
        break;
    default:
        console.log("not found"); // not found
}

If the value arrives as a string and the case values are numbers, convert the string to a number first. With the header switch (Number(input)), the program prints two.

Equality is the only check a case makes. Ranges such as score >= 90 are checked with an if and else if chain.

Common mistake

A branch is missing its break:

JavaScript
const level = 1;

switch (level) {
    case 1:
        console.log("beginner"); // ✗
    case 2:
        console.log("advanced");
        break;
}

When level is 1, the program prints two lines: beginner and advanced. There is no error: falling through is allowed, and JavaScript gives no warning about a missing break.

After console.log("beginner"), execution falls through into case 2 and stops only at break. Put break at the end of every branch that must not fall through.

Key points

  • switch evaluates the expression once and starts running at the matching case, or at default if none matches.
  • A branch ends at break or return; without them, execution falls through into the next case.
  • case compares strictly, with ===: the string "2" will not match the number 2.
  • Ranges of values are checked with an if and else if chain, not with switch.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way