Some statements (switch case ...)

switch statement in each case (case) means: "If the expression is equal to the value (value), then the back of the execution
statement (statement)". The break keyword will lead to code execution flow out of the switch statement. If you omit the break keyword,
after completion will lead to the implementation of the current case, continue with the next case. The final key is used before the default expression does not match
when the surface either case, the code is executed maneuver (and thus, also corresponds to an else statement).

Its advantage is that you can replace the emergence of a variety if else if the judge sentences

. 1  var NUM =. 8 ;
 2  Switch (NUM) {
 . 3      Case . 8 :
 . 4          Alert (. 8 );
 . 5          NUM =. 7 ;
 . 6          the console.log (NUM);
 . 7          BREAK ;
 . 8      Case . 1: Alert (. 1 );
 . 9          BREAK ;
 10      case 2: Alert (2 );
 . 11          BREAK ;
 12 is      case 3 :
 13 is      // merge num equal to 3 or 4 of the case 
14      case 4: Alert (3,4- );
 15         BREAK ;
 16      Case . 5: Alert (. 5 );
 . 17          BREAK ;
 18 is      default : Alert ( "mismatch" );
 . 19          BREAK ;
 20 is }

Although ECMAScript switch statement borrowed from other languages, but this statement also has its own characteristics. First, you can
use a switch statement in any data type (numeric only use in many other languages), whether it is a string, or objects have no
problem. Second, the value of each case is not necessarily constant, it can be a variable, and even expressions.

 1 switch ("hello world") {
 2     case "hello" + " world":
 3         alert("Greeting was found.");
 4         break;
 5     case "goodbye":
 6         alert("Closing was found.");
 7         break;
 8     default:
 9         alert("Unexpected message was found.");
10 }

Analyzing Boolean case:

 1 var num = 25;
 2 switch (true) {
 3     case num < 0:
 4         alert("Less than 0.");
 5         break;
 6     case num >= 0 && num <= 10:
 7         alert("Between 0 and 10");
 8         break;
 9     case num > 10 && num <= 20:
10         alert("Between 10 and 20");
11         break;
12     default:
13         alert("More than 20");
14 }

Each case returns a Boolean value, so the switch to convey the expression is true;

switch statement used in the comparison value is congruent operator, the type conversion does not occur (e.g.,
the string "10" is not equal to the value 10)

Guess you like

Origin www.cnblogs.com/LeeeOooonHuang/p/11493034.html