The switch Statement
switch compares one value with several case labels. Each case should end with break, otherwise execution falls through into the next case. default runs when nothing matches.
For ranges and complex conditions, if / else if is clearer than switch.
Example
const day = 3;
let name;
switch (day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
default:
name = "Another day";
}
console.log(name);Exercise
Stop the switch from falling through.
case 1: console.log("one"); ;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.