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.
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;
}Go shopping
| With if … else if | With switch |
|---|---|
| if (day == "Saturday") | case "Saturday": |
| else if (day == "Sunday") | case "Sunday": |
| else | default: |
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.
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:let day = "Saturday";
switch (day) {
case "Saturday":
console.log("Go to lessons");
case "Sunday":
console.log("Go shopping");
break;
}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:
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;
}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:
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;
}Give it to Mom and Dad
case ends with a colon :, not a semicolon. Text values go in quotes case "A":, while numbers do not case 0:.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.
let day = "Sunday";
___ (___) {
___ "Saturday":
console.log("Go to lessons");
___;
case "Sunday":
console.log("Go shopping");
break;
___:
console.log("Go to school");
break;
}Press Run to see the result. Complete the switch on letter: "A" prints Well done!, "B" prints Keep up the good work, "C" prints Let's try a little harder.
let letter = "B";
switch ( ) {
}Press Run to see the result. Add a default case that prints The notation is incorrect when no case matches.
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;
}Press Run to see the result. Using switch on the remainder of number divided by 3: remainder 0 prints Group A, 1 prints Group B, 2 prints Group C.
Hint
Putnumber % 3 in the brackets of switch.let number = 20;
switch ( ) {
}Press Run to see the result. 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!
let dayCount = 11;
switch ( ) {
}Press Run to see the result. You ordered juice but the program prints two lines. Fix it so it prints only Orange juice: 15 pounds.
Hint
Which case is missingbreak?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;
}Press Run to see the result. 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.
let floor = 2;
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 liken % 3in the brackets.