The switch
statement evaluates an expression, matching the expression’s value to a case
clause, and executes statements associated with that case
, as well as statements in case
s that follow the matching case
.
const fruit = 'Papayas';
switch (fruit) {
case 'Oranges':
console.log('Oranges are $0.59 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 ${fruit}.`);
}
// Mangoes and papayas are $2.79 a pound.