charat javascript in swicth code example
Example 1: 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 2: charat javascript in swicth
function getLetter(s) {
let letter;
switch (true) {
case "aeiou".includes(s[0]):
letter = "A";
break;
case "bcdefg".includes(s[0]):
letter = "B";
break;
case "hijklm".includes(s[0]):
letter = "C";
break;
case "nopqrstuvwxyz".includes(s[0]):
letter = "D";
break;
}
return letter;
}
Example 3: how to use switch case in javascript
switch (expression) {
case value1:
[break;]
case value2:
[break;]
...
case valueN:
[break;]
[default:
[break;]]
}