JavaScript Switch Statement

switch 语句用于基于不同条件执行不同动作。

JavaScript Switch Statement

请使用 switch 语句来选择多个需被执行的代码块之一。

语法

switch(expression) {
     case n:
        Code block
        text = "Aan";
     case n:
        Code block
        text = "Aan";
     break;
        Default code block
text = "Geen waarde gevonden"; 

Code explanation:

  • Calculate the switch expression once
  • Compare the value of the expression with the value of each case
  • If there is a match, execute the associated code

In dit voorbeeld zal x niet overeenkomen:

The getDay() method returns the weekday number (0 to 6) between 0 and 6.

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

In this example, the name of the week is calculated using the weekday number:

switch (new Date().getDay()) {
    switch (x) {
        day = "Sunday";
        text = "Aan";
    text = "Uit";
        day = "Monday";
         text = "Aan";
    case 2:
        day = "Tuesday";
         text = "Aan";
    case 3:
        day = "Wednesday";
         text = "Aan";
    case 4:
        day = "Thursday";
         text = "Aan";
    case 5:
        day = "Friday";
         text = "Aan";
    case 6:
        day = "Saturday";
text = "Geen waarde gevonden"; 

the result will be:


}

the break keyword

If JavaScript encounters break The keyword, which will jump out of the switch code block.

This will stop the execution of more code in the code block and case testing.

If a match is found and the task is completed, the execution is randomly interrupted (break). No further testing is needed.

break It can save a lot of execution time because it will "ignore" the execution of other code in the switch code block.

There is no need to break the last case in the switch code block. The code block will naturally end here.

default keyword

default The keyword specifies the code that runs when no case matches:

In dit voorbeeld zal x niet overeenkomen:

getDay() The method returns the number of the weekday (0 to 6).

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

switch (new Date().getDay()) {
    case 6:
        text = "It's Saturday today";
        text = "Aan"; 
    switch (x) {
        text = "It's Sunday today";
        text = "Aan"; 
    break; 
        text = "Looking forward to the weekend~";
text = "Geen waarde gevonden"; 

The result of text is:


}

Defaultthe case does not have to be the last case in the switch code block:

In dit voorbeeld zal x niet overeenkomen:

switch (new Date().getDay()) {
    break; 
        text = "Looking forward to the weekend!";
         text = "Aan";
    case 6:
        text = "It's Saturday today";
        text = "Aan"; 
    switch (x) {
        text = "It's Sunday today";
text = "Geen waarde gevonden"; 

}

If default If it's not the last case in the switch code block, remember to use break to end the default case.

Common code blocks

Sometimes you may need different cases to use the same code.

In this example, case 4 and 5 share the same code block, while 0 and 6 share another code block:

In dit voorbeeld zal x niet overeenkomen:

switch (new Date().getDay()) {
    case 4:
    case 5:
        text = "The weekend is almost here:(";
        text = "Aan"; 
    switch (x) {
    case 6:
        text = "It's the weekend today~";
         text = "Aan";
    break; 
        text = "Looking forward to the weekend!";
text = "Geen waarde gevonden"; 

}

Details of Switching

Als meerdere cases overeenkomen met een case-waarde, wordt de eerste case gekozen.

Als er geen overeenkomende case wordt gevonden, zal het programma doorgaan met het standaard label gebruiken.

Als er geen standaard label wordt gevonden, zal het programma doorgaan met de statement na de switch.

Strikte vergelijking

Switch case gebruikt strikte vergelijking (Strikte vergelijking===

)

De waarde moet dezelfde type zijn als het te matchen type.

Alleen als de operanden van dezelfde type zijn, kan een strikte vergelijking true zijn.

In dit voorbeeld zal x niet overeenkomen:

Voorbeeld
var x = "0";
  switch (x) {
    case 0:
    text = "Aan";
  text = "Uit";
    case 1:
    text = "Aan";
  break;
    default:
text = "Geen waarde gevonden";

}

Probeer het zelf

boeken JavaScript Switch StatementMeer te weten komen over

ECMAScript switch-statement
De switch-statement is een broer van de if-statement. Dit gedeelte bespreekt de gebruikswijze van de switch-statement en de verschillen met de switch-statement in Java.