3 Example(s) of JavaScript Switch Case statement


Description :

In this example, We have two variables grade with value as "A" and message as blank and we will pass this grade to switch statement and this will result back with message as "Excellent". here is the JavaScript switch statement: switch (grade) { case 'A': message = "Excellent"; break; case 'B': message = "Good"; break; case 'C': message = "OK"; break; case 'D': message = "Mmmmm...."; break; case 'E': message = "You must do better than this"; break; default: message = "What is your grade anyway?"; break; } See the code snippet and its output:


JavaScript Switch Case statement Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var grade = 'A';
var message = '';
switch (grade) {
    case 'A':
        message = "Excellent";
        break;
    case 'B':
        message = "Good";
        break;
    case 'C':
        message = "OK";
        break;
    case 'D':
        message = "Mmmmm....";
        break;
    case 'E':
        message = "You must do better than this";
        break;
    default: 
        message = "What is your grade anyway?";
        break;
}
document.getElementById("sample").innerHTML =  message;
</script>

</body>
</html>

Output

Description :

In this example of JavaScript switch statement, we will pass new Date().getDay() to switch statement and if the value is 0 it will return Sunday and if value is 6 it will return "Saturday" if the values is not 0 or 6 then it will execute default test case and output as "Looking forward to the Weekend" See the code snippet:


JavaScript Switch Case statement Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var text;
switch (new Date().getDay()) {
    case 6:
        text = "Today is Saturday";
        break;
    case 0:
        text = "Today is Sunday";
        break;
    default:
        text = "Looking forward to the Weekend";
}
document.getElementById("sample").innerHTML = text;
</script>
</body>
</html>

Output

Description :

In this example of JavaScript switch statement, we have 4 cases without break statement case 10: output += 'So '; case 1: output += 'What '; output += 'Is '; case 2: output += 'Your '; case 3: output += 'Name'; So if we pass value as 1 in the switch statement and execute all the statements till it find break statement and the output will be "What Is Your Name"? See the code snippet:


JavaScript Switch Case statement Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>   
<body>
<p id="sample"></p>
<script>
var foo = 1;
var output = 'Output: ';
switch (foo) {
  case 10:
    output += 'So ';
  case 1:
    output += 'What ';
    output += 'Is ';
  case 2:
    output += 'Your ';
  case 3:
    output += 'Name';
  case 4:
    output += '?';
    break;
  case 5:
    output += '!';
     break;
  default:
   output+='This is default value.';
}
document.getElementById("sample").innerHTML = output;
</script>
</body>
</html>

Output