The switch statement is an alternative to long if/else if chains when you are matching a single value against several discrete options.
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code when no case matched
}
Important: Always add
breakat the end of each case. Without it, execution "falls through" to the next case.
let day = "Saturday";
switch (day) {
case "Saturday":
case "Sunday":
console.log("Weekend!");
break;
default:
console.log("Weekday");
}
getDayType(day) that accepts a day name (e.g. "Monday") and returns:
"Weekend" for "Saturday" or "Sunday""Weekday" for any other day name"Unknown" if the input is not a valid day namegetDayType("Saturday") and display the result in #output as "Saturday: Weekend".Each case matches the value after switch (expression).