switch function code example
Example 1: switch javascript
switch (a) {
case 1:
alert('case 1 executed');
break;
case 2:
alert("case 2 executed");
break;
case 3:
alert("case 3 executed");
break;
case 4:
alert("case 4 executed");
break;
default:
alert("default case executed");
}
Example 2: 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);
Example 3: switch function
var rand = Math.round(random(1,4));
switch(rand){
case 1: console.log("Chosen Number : 1");
break;
case 2: console.log("Chosen Number : 2");
break;
case 3: console.log("Chosen Number : 3");
break;
case 4: console.log("Chosen Number : 4");
default: break;
}
Example 4: switch statement
int x = Random.Range(0, 5);
switch(x){
case 0:
break;
case 1:
break;
}