Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
switch 語句會比對一個表達式裡頭的值是否符合 case 條件,然後執行跟這個條件相關的陳述式,以及此一符合條件以外,剩下其他條件裡的陳述式。
一個表達式其結果用來跟每個 case 條件比對。
case valueN 選擇性一個 case 條件是用來跟 expression 匹配的。 如果 expression 符合特定的 valueN,那在 case 條件裡的語句就會執行,直到這個 switch 陳述式結束或遇到一個 break 。
default 選擇性一個 default 條件;倘若有這個條件,那在 expression 的值並不符合任何一個 case 條件的情況下,就會執行這個條件裡的語句。
一個 switch 陳述式會先評估自己的 expression。然後他會按照 case 條件順序開始尋找,直到比對到第一個表達式值跟輸入 expression 的值相等的 case 條件(使用嚴格的邏輯運算子, ===)並把控制流交給該子句、並執行裡面的陳述式(如果給定值符合多個 case,就執行第一個符合的 case,就算該 case 與其他 case 不同)
If no matching case clause is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. 按照慣例, default 語句會是最後一個條件,但不一定要存在。
The optional break statement associated with each case label 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 the following example, if expr evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program breaks out of switch and executes the statement following switch. If break were omitted, the statement for case "Cherries" would also be executed.
If you forget a break then the script will run from the case where the criterion is met and will run the case after that regardless if criterion was met. See example here:
Yes, you can! JavaScript will drop you back to the default if it can't find a match:
It also works when you put default before all other cases.
Source for this technique is here:
Switch statement multiple cases in JavaScript (Stack Overflow)
This method takes advantage of the fact that if there is no break below a case statement it will continue to execute the next case statement regardless if the case meets the criteria. See the section titled "What happens if I forgot a break?"
This is an example of a single operation sequential switch statement, where four different values perform exactly the same.
This is an example of a multiple-operation sequential switch statement, where, depending on the provided integer, you can receive different output. This shows you that it will traverse in the order that you put the case statements, and it does not have to be numerically sequential. In JavaScript, you can even mix in definitions of strings into these case statements as well.
The output from this example:
| foo is NaN or not 1, 2, 3, 4, 5 or 0 | Please pick a number from 0 to 5! |
| 0 | Output: So What Is Your Name? |
| 1 | Output: What Is Your Name? |
| 2 | Output: Your Name? |
| 3 | Output: Name? |
| 4 | Output: ? |
| 5 | Output: ! |
With ECMAScript 2015 (ES6) support made available in most modern browsers, there will be cases where you would want to use let and const statements to declare block-scoped variables.
Take a look at this example:
This example will output the error Uncaught SyntaxError: Identifier 'message' has already been declared which you were not probably expecting.
This is because the first let message = 'hello'; conflicts with second let statement let message = 'hi'; even they're within their own separate case statements case 'say_hello': and case 'say_hi':; ultimately this is due to both let statements being interpreted as duplicate declarations of the same variable name within the same block scope.
We can easily fix this by wrapping our case statements with brackets:
This code will now output hello in the console as it should, without any errors at all.
| ECMAScript® 2027 Language Specification # sec-switch-statement |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月16日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.