JavaScript-Switch-Anweisung
- Vorherige Seite JS-Bedingung
- Nächste Seite JS-Schleife For
switch
语句用于基于不同条件执行不同动作。
JavaScript-Switch-Anweisung
请使用 switch
语句来选择多个需被执行的代码块之一。
语法
switch(expression) { case n: Code block unterbrechen; case n: Code block unterbrechen; Standard: Default code block }
Code explanation:
- Calculate the switch expression once
- Compare the value of the expression with the value of each case
- If there is a match, then execute the associated code
Beispiel
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()) { Fall 0: day = "Sunday"; unterbrechen; Fall 1: day = "Monday"; unterbrechen; case 2: day = "Tuesday"; unterbrechen; case 3: day = "Wednesday"; unterbrechen; case 4: day = "Thursday"; unterbrechen; case 5: day = "Friday"; unterbrechen; case 6: day = "Saturday"; }
The result will be:
break keyword
If JavaScript encounters break
keyword, which will jump out of the switch code block.
This will stop the execution of more code in the code block as well as the case testing.
If a match is found and the task is completed, then 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:
Beispiel
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"; unterbrechen; Fall 0: text = "It's Sunday today"; unterbrechen; Standard: text = "Looking forward to the weekend~"; }
The result of text is:
Defaultthe case does not have to be the last case in the switch code block:
Beispiel
switch (new Date().getDay()) { Standard: text = "Looking forward to the weekend!"; unterbrechen; case 6: text = "It's Saturday today"; unterbrechen; Fall 0: text = "It's Sunday today"; }
If default
If it is 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:
Beispiel
switch (new Date().getDay()) { case 4: case 5: text = "The weekend is approaching:("; unterbrechen; Fall 0: case 6: text = "It's the weekend today~"; unterbrechen; Standard: text = "Looking forward to the weekend!"; }
Details of Switching
Wenn mehrere Fälle einen Fallwert matchen, wird der erste Fall gewählt.
Wenn kein passender Fall gefunden wird, führt das Programm fort, indem es das Standard-Label verwendet.
Wenn kein Standard-Label gefunden wird, führt das Programm fort, nachdem der switch beendet wurde.
strenge Vergleich
Switch case verwendet eine strenge Vergleichsbedingung (===
)
Der Wert muss mit dem zu matchenden Typ übereinstimmen.
Nur wenn Operanden desselben Typs sind, kann eine strikte Vergleichsbedingung wahr sein.
In diesem Beispiel wird x nicht übereinstimmen:
Beispiel
var x = "0"; switch (x) { Fall 0: text = "Ausschaltet"; unterbrechen; Fall 1: text = "Eingeschaltet"; unterbrechen; Standard: text = "Kein Wert gefunden"; }
Fachbücher
möchten Sie mehr über JavaScript-Switch-Anweisungweitere Informationen, lesen Sie bitte die entsprechenden Inhalte im JavaScript-Advanced-Tutorial:
- ECMAScript switch-Anweisung
- Die switch-Anweisung ist ein Bruder der if-Anweisung. Dies Kapitel beschreibt die Verwendung der switch-Anweisung und die Unterschiede zur switch-Anweisung in Java.
- Vorherige Seite JS-Bedingung
- Nächste Seite JS-Schleife For