JavaScript / Making decisions

The switch statement

Choose between many values of one variable with a switch statement instead of a long else if chain.

When you compare one variable with many values, an else if chain gets long and repetitive. A switch statement says the same thing more clearly: one value to check, and a list of possible cases.

JavaScript
let day = "Sunday";

switch (day) {
  case "Saturday":
    console.log("Go to lessons");
    break;
  case "Sunday":
    console.log("Go shopping");
    break;
  default:
    console.log("Go to school");
    break;
}
Console
Go shopping
With if … else ifWith switch
if (day == "Saturday")case "Saturday":
else if (day == "Sunday")case "Sunday":
elsedefault:

The computer takes the value in the brackets of switch and compares it with each case from top to bottom. At the first match it runs the lines below it, and break takes it out of the whole statement.

If you forget break, the program does not stop; it carries on into the next case and runs it too. Here day is "Saturday" but two lines are printed:
JavaScript
let day = "Saturday";

switch (day) {
  case "Saturday":
    console.log("Go to lessons");
  case "Sunday":
    console.log("Go shopping");
    break;
}
Console
Go to lessons
Go shopping

The default case

default runs when the value matches no case, just like else in an if statement. It usually goes at the end:

JavaScript
let bloodType = "Unknown";

switch (bloodType) {
  case "A":
    console.log("Today's fortune is decent");
    break;
  case "B":
    console.log("Today's fortune is excellent!");
    break;
  default:
    console.log("Let's find out your blood type!");
    break;
}
Console
Let's find out your blood type!

A calculation inside switch

You can put a calculation in the brackets of switch, such as the remainder %. Sharing 20 donuts between 3 people leaves 0, 1 or 2, and each remainder gets a different action:

JavaScript
let donuts = 20;

switch (donuts % 3) {
  case 0:
    console.log("Divided equally");
    break;
  case 1:
    console.log("Give it to Mom");
    break;
  case 2:
    console.log("Give it to Mom and Dad");
    break;
}
Console
Give it to Mom and Dad
Each case ends with a colon :, not a semicolon. Text values go in quotes case "A":, while numbers do not case 0:.

Practice

1Fill in the blankCurriculum practice

Fill in the blanks to make a switch statement on day: "Saturday" prints Go to lessons, "Sunday" prints Go shopping, anything else prints Go to school.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let day = "Sunday";

___ (___) {
  ___ "Saturday":
    console.log("Go to lessons");
    ___;
  case "Sunday":
    console.log("Go shopping");
    break;
  ___:
    console.log("Go to school");
    break;
}
Console
Press Run to see the result.
2Write the programCurriculum practice

Complete the switch on letter: "A" prints Well done!, "B" prints Keep up the good work, "C" prints Let's try a little harder.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let letter = "B";

switch ( ) {

}
Console
Press Run to see the result.
3Edit the programCurriculum practice

Add a default case that prints The notation is incorrect when no case matches.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let letter = "D";

switch (letter) {
  case "A":
    console.log("Well done!");
    break;
  case "B":
    console.log("Keep up the good work");
    break;
  case "C":
    console.log("Let's try a little harder");
    break;
}
Console
Press Run to see the result.
4Write the programCurriculum practice

Using switch on the remainder of number divided by 3: remainder 0 prints Group A, 1 prints Group B, 2 prints Group C.

HintPut number % 3 in the brackets of switch.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let number = 20;

switch ( ) {

}
Console
Press Run to see the result.
5Write the programCurriculum practice

Using switch on the remainder of dayCount divided by 7: remainder 0 prints Let's eat a hamburger!, remainder 4 prints Let's eat steak!

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let dayCount = 11;

switch ( ) {

}
Console
Press Run to see the result.
6Edit the programExtra practice

You ordered juice but the program prints two lines. Fix it so it prints only Orange juice: 15 pounds.

HintWhich case is missing break?
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let order = "juice";

switch (order) {
  case "tea":
    console.log("Tea: 10 pounds");
    break;
  case "juice":
    console.log("Orange juice: 15 pounds");
  case "cake":
    console.log("Cake: 25 pounds");
    break;
}
Console
Press Run to see the result.
7Write the programExtra practice

In the school lift, floor = 2. Write a switch: 0 prints Reception, 1 prints Library, 2 prints Computer lab, any other number prints No such floor.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let floor = 2;
Console
Press Run to see the result.

Key points

  • switch (x) { case v: … break; } compares one variable with many values.
  • Without break, the program runs on into the next case.
  • default: runs when no case matches, and you can put a calculation like n % 3 in the brackets.