if else switch case javascript code example
Example 1: switch js
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Apples':
console.log('Apples are $0.32 a pound.');
break;
case 'Bananas':
console.log('Bananas are $0.48 a pound.');
break;
case 'Cherries':
console.log('Cherries are $3.00 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
break;
default:
console.log('Sorry, we are out of ' + expr + '.');
}
console.log("Is there anything else you'd like?");
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;
}