In JavaScript, you can substitute the if...else
statement with a switch
statement.
The function below is an if-else statement:
function chainToSwitch(val){
let answer = "";
if (val === "hip hip") {
answer = "hooray!"
} else if (val === 7) {
answer = "Ate 9"
} else if (val === "Michael") {
answer = "Jackson"
} else {
answer = "Not this time!"
}
return answer;
}
The function below is a switch statement:
function switchedChain(val){
let answer = "";
switch (val) {
case "hip hip":
answer = "hooray!";
break;
case 7:
answer = "Ate 9";
break;
case "Michael":
answer = "Jackson"
break;
default :
answer = "Not this time!"
break;
}
return answer;
}