Javascript - Write program to switch on string value

Requirements

Write program to switch on string value

The following switch statement does not work.


var text;
var favCar = "Json";

switch(favCar) {
    case "Json":
      text = "Json";
    case "A":
      text = "A";
    case "P":
      text = "P";
    default:
      text = "Unknown car name";
}
console.log( text );

Here is the fix

Demo

var text;
var favCar = "Json";

switch(favCar) {/*from  ww  w  .  j av  a 2 s. co m*/
    case "Json":
      text = "Json";
      break;
    case "A":
      text = "A";
      break;
    case "P":
      text = "P";
      break;
    default:
      text = "Unknown car name";
}
console.log( text );

Result

Related Exercise