JavaScript Switch Statements
Switch Statements are another way to write conditional code. Depending on the condition, a different code block gets executed.
Example
switch(fruit) {
case "mango":
console.log("Mango is Candy's favorite fruit!");
break;
case "guava":
console.log("Mmm Mmm guava!");
break;
case "banana":
console.log("I am bananas for bananas");
break;
default:
console.log("You didn't choose the right fruit!");
}
What would the output be in each case?
(1)
var fruit = "banana";
(2)
var fruit = "mango";
(3)
var fruit = "guava";
(4)
var fruit = "orange";
(5)
var fruit = "Mango";
Answers
- I am bananas for bananas
- Mango is Candy's favorite fruit!
- Mmm Mmm guava!
- You didn't choose the right fruit!
- You didn't choose the right fruit!