lisa
0
Q:

switch statement javascript

switch (a) {
    case 1:
        alert('case 1 executed');
        break;
    case 2:
        alert("case 2 executed");
        break;
   case 3:
        alert("case 3 executed");
        break;
    case 4:
        alert("case 4 executed");
        break;
    default:
        alert("default case executed");
}
25
//javascript multiple case switch statement
var color = "yellow";
var darkOrLight="";
switch(color) {
    case "yellow":case "pink":case "orange":
        darkOrLight = "Light";
        break;
    case "blue":case "purple":case "brown":
        darkOrLight = "Dark";
        break;
    default:
        darkOrLight = "Unknown";
}

//darkOrLight="Light"
58
var myvalue='val1';//or other values; val2 val3 valx
switch(myvalue) {
  case 'val1':
      	console.log('var myvalue is '+ myvalue);
    	//other code ...
    break;
  case 'val2':
    	console.log('var myvalue is '+ myvalue);
    	//other code ...
    break;
  default:
    	// when all the other values not covered with cases above
    	// other code ...
}
1
let color = "black";

switch(color){
    case "red":
        console.log("color is red");
        break;
    case "white":
        console.log("color is white");
        break;
    case "black":
        console.log("color is black");
        break;
    default:
        console.log("unknow color");
}
2
function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
switch(val) {
  case 1:
    answer = "alpha";
    console.log(answer);
    break;
  case 2:
    answer = "beta";
    break;
  case 3:
    answer = "gamma";
    break;
  case 4:
    answer = "delta";
    break;
}


  // Only change code above this line
  return answer;
}

caseInSwitch(1);
5

switch(expression) {

  case x:

     // code block

        break;

  case y:

     // code block

     break;

  default:

      // code block

 }
 
3
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
44
var text;
var fruits = document.getElementById("myInput").value;

switch(fruits) {
  case "Banana":
    text = "Banana is good!";
    break;
  case "Orange":
    text = "I am not a fan of orange.";
    break;
  case "Apple":
    text = "How you like them apples?";
    break;
  default:
    text = "I have never heard of that fruit...";
}
0
switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
     day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case 6:
    day = "Saturday";
}
0

New to Communities?

Join the community