Home of web learners

www.webn3rd.com

Switch is used to execute within condition value

Lets do an example

<html>
<head>
    
</head>
<body>
    <script>
    var a=2;
    switch (a) {
      case 1:
          document.write('This is two');
          break;
          case 3:
                document.write('This is three');
          break;
          default:
                document.write('This is not matched');
         
    }
  </script>
    
</body>
</html>

Here you see that inside the switch,we use the variable which we are going to check,next we use the case,if this case value matched with the variable value,then those case are going to execute

In this example,it matched with 2,so its shows this is two,then program got break statement and exit from the switch

If no case not matched with the variable value,then it execute default block,here it having a code for showing text,and exit from the switch

webn3rd.com

About webn3rd