Created
October 20, 2014 05:49
-
-
Save lizlongnc/ce03a5fb487c08d748ae to your computer and use it in GitHub Desktop.
JS switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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