Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Created October 20, 2014 05:49
Show Gist options
  • Save lizlongnc/ce03a5fb487c08d748ae to your computer and use it in GitHub Desktop.
Save lizlongnc/ce03a5fb487c08d748ae to your computer and use it in GitHub Desktop.
JS switch
use switch statement when if else statements accumulate
var expr = 'Oranges';
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?");
results:
Oranges are $0.59 a pound. VM380:5
Is there anything else you'd like?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment