Switch-1

 

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:

 
 
 
<!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