Soft technology web tutorial: JavaScript Switch Statement

switch statement based on different conditions to perform different actions.

JavaScript Switch Statement

Use the  switch statement to select one of the plurality of code blocks need to be performed.

grammar

Switch (expression) {
      Case n-: 
        block 
        BREAK ;
      Case n-: 
        block 
        BREAK ;
      default : 
        default block 
}

 

Code explanation:

  • Calculated once the switch expression
  • The value of the expression is compared with the value for each case of
  • If there is a match, then the association code execution

Examples

getDay () method returns the name of the circumferential number between 0 and 6 (weekday number).

(Sunday=0, Monday=1, Tuesday=2 ..)

This example uses numbers to calculate the circumferential periphery of the name Name:

Switch ( new new . a Date () getDay ()) {
     Case 0 : 
        Day = "Sunday" ;
         BREAK ;
     Case . 1 : 
        Day = "Monday" ;
          BREAK ;
     Case 2 : 
        Day = "Tuesday" ;
          BREAK ;
     Case . 3 : 
        Day = "Wednesday" ;
          BREAK ;
     Case 4 : 
        Day = "Thursday" ;
          BREAK ;
    Case . 5 : 
        Day = "Friday" ;
          BREAK ;
     Case . 6 : 
        Day = "Saturday" ; 
}

 

The result will be:

Today is Saturday

break Keywords

If you encounter JavaScript  break keyword, it will jump out of the switch code block.

This will stop the code block code execution and more test case.

If a match is found, and to complete the task, the random interrupt execution (break). No further testing.

break execution can save a lot of time, because it will "ignore" execute other code switch code block.

The last case switch without interrupting the code block. Code block will naturally end here.

default Keywords

default case when the code matches a predetermined keyword running does not exist:

Examples

getDay () method returns the number of weeks 0-6 name.

If today is neither a Saturday (6) nor Sunday (0), the output for a default message:

Switch ( new new a Date () getDay ().) {
     Case 6 : 
        text = "Today is Saturday" ;
         BREAK ; 
     Case 0 : 
        text = "Today is Sunday" ;
         BREAK ; 
     default : 
        text = "~ looking forward to the weekend" ; 
}

 

The text is the result:

Today is Saturday

The default case is not necessarily the last switch block case:

Examples

Switch ( new new . a Date () getDay ()) {
     default : 
        text = "looking forward to the weekend!" ;
          BREAK ;
     Case 6 : 
        text = "Today is Saturday" ;
         BREAK ; 
     Case 0 : 
        text = "Today is Sunday" ; 
}

If the  default is not a switch block in the last case, remember to end with a break default case.

Common code block

Sometimes you need a different case to use the same code.

In the present embodiment, case 4, and 5 share the same code block, and 0 and 6 share another code block:

Examples

Switch ( new new . a Date () getDay ()) {
     Case . 4 :
     Case . 5 : 
        text = "weekend coming :)" ;
         BREAK ; 
     Case 0 :
     Case . 6 : 
        text = "Today is the weekend ~" ;
          BREAK ;
     default : 
        text = "looking forward to the weekend!" ; 
}

Switching details

If a plurality of case matching case value, the first case is selected.

If a matching case is found, the program will continue to use the default label.

If the default label, the program will continue after the switch statement was not found.

Strictly comparable

Switch case a strict comparison ( ===).

Value must be the same type to be matched.

Only operands of the same type, a strict comparison can be true.

In this case, x will not match:

Examples

var x = "0";
switch (x) {
  case 0:
    text = "Off";
    break;
  case 1:
    text = "On";
    break;
  default:
    text = "No value found";
}

Source: www.sysoft.net.cn, plus v: 15844800162 depth exchange

Guess you like

Origin www.cnblogs.com/sysoft/p/12000856.html