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

  1. I am bananas for bananas
  2. Mango is Candy's favorite fruit!
  3. Mmm Mmm guava!
  4. You didn't choose the right fruit!
  5. You didn't choose the right fruit!