javaScript switch case statement

enables the execution of one or more statements when a specified expression's value matches a label.

A javascript switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement.

syntax:

         switch(expression){
    
                       case lable_1: statements;
                
                                  break;
                                              
                      case  lable_2: statements;
                
                                  break;
                  
                      case  lable_n: statements;
                
                                  break;
                                                              
                      default: statements;
                
                      }

the default and beak keyword inside switch is an optional.

the switch case labels must end with a colon(:)

the case labels are constant expressions whose values must be unique,meaning no duplicate values are allowed.

the break statement indicate to the interpreter the end of that particular case. if they were omitted, the interpreter would continue executing each statement in each of the following case.

  <script>
     
           var   fruit  = "apple";
           
       switch (fruit){
                 
                           case  'apple':alert("you like  apple");
                        
                        
                           case  'orange':alert("you like orange");break;
                        
                        
                           case  'grap': alert("you like grap");break;
                        
                        
                           default :alert("you not like fruit");
                    
                       }
  </script>
  <script>
  
         var    digit  = 3;
            
                      
         switch(digit){
               
                        case   1:   document.write("<br/>"+"the      1     digit");
                                   
                        case   2:   document.write("<br/>"+"the      2     digit");break;
                
                        case   3:   document.write("<br/>"+"the      3     digit");
                                   
                        case   4:   document.write("<br/>"+"the      4     digit");
                                   
                        case   5:   document.write("<br/>"+"the      5     digit");break;
                                   
                    }  
 </script>

how it work

  • first the value of the switch expression is compared with the values for each case in the    structure.if there is a match, the block of code associated with that case is executed.

  • if no case label is equal to the value of the switch expression,the statement associated    with the default label is executed.

  • the optional break statement associated with each case clause ensures that the    program breaks out of switch once the matched statement is executed and continues    execution at the statement following switch.

  • if break is omitted, the program continues execution at the next statement in the switch    statement.


in javascript - what if switch expression value never match with any case value in switch structure.

in this condition always default statement will be executed.

  <script>
  
         var    digit  = 33;
            
                      
  switch(digit){
               
                    case   1:    alert("<br/>"+"the______1_____digit");
                                   
                    case   2:    alert("<br/>"+"the______2_____digit");break;
                
                    case   4:    alert("<br/>"+"the______4_____digit");
                                   
                   default:                                     
                  
                 document.write("<br/>"+"please enter only digit between 1 to 4");
          }
            
 </script>

nested switch statement in javaScript

in javascript switch statement can be nested.in cases, case or default labels associate with the most deeply nested switch case statements that enclose them.look in the blow example

    <head>
    
      <script>   var  a = 'two'; var b="four";
        
          switch(a){  case 'one':alert("one case statement");
          
                          case 'two':alert("two case statement");
                      
                            switch(b){
                            
                             case "three": alert("nested switch third case statement");
                                                 
                             case "four":  alert("nested switch four  case statement");
                                                
                            }
                                               
                   } </script></head>