writing switch functions javascript code example
Example 1: javascript switch statement multiple cases
var color = "yellow";
var darkOrLight="";
switch(color) {
case "yellow":case "pink":case "orange":
darkOrLight = "Light";
break;
case "blue":case "purple":case "brown":
darkOrLight = "Dark";
break;
default:
darkOrLight = "Unknown";
}
Example 2: Javascript switch case code format
switch(type) {
case "SomeString":
break;
case "OtherString":
break;
default:
break;
}
Example 3: switch statement javascript
function caseInSwitch(val) {
var answer = "";
switch(val) {
case 1:
answer = "alpha";
console.log(answer);
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
break;
}
return answer;
}
caseInSwitch(1);