codescracker
javascript

JavaScript switch Statement



« Previous Tutorial Next Tutorial »

The switch statement in JavaScript is used to select a particular group of statements to be executed among several other groups of statements.

JavaScript switch Statement Example

Here is an example illustrates how to use switch statement in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
   <title>JavaScript switch Statement</title>
</head>
<body>

<h3>JavaScript switch Statement Example</h3>
<script type="text/javascript">
   
   var mychar = "C";
   document.write("Checking the character...<br/>");
   switch(mychar)
   {
      default: document.write(mychar + " is a consonant.");
         break;
      case "A": document.write(mychar + " is a vowel.");
         break;
      case "E": document.write(mychar + " is a vowel.");
         break;
      case "I": document.write(mychar + " is a vowel.");
         break;
      case "O": document.write(mychar + " is a vowel.");
         break;
      case "U": document.write(mychar + " is a vowel.");
         break;
   }
   document.write("<br/>Done!");
   
</script>

</body>
</html>

Here is the sample output of the above JavaScript switch statement example:

javascript switch statement

JavaScript Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker