この翻訳は不完全です。英語から この記事を翻訳 してください。
式を評価し、その式の値が case のラベルと一致するなら、その case に関連付けられた文を実行します。
構文
switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
Approved English (US) version:
- JavaScript
- Tutorials:
- JavaScript Guide
- Introduction
- Grammar and types
- Control flow and error handling
- Loops and iteration
- Functions
- Expressions and operators
- Numbers and dates
- Text formatting
- Regular expressions
- Indexed collections
- Keyed collections
- Working with objects
- Details of the object model
- Iterators and generators
- Meta programming
- Introductory
- Intermediate
- Advanced
- References:
- Built-in objects
- Global Objects
- Array
- ArrayBuffer
- Boolean
- DataView
- Date
- Error
- EvalError
- Float32Array
- Float64Array
- Function
- Generator
- GeneratorFunction
- Infinity
- Int16Array
- Int32Array
- Int8Array
- InternalError
- Intl
- Intl.Collator
- Intl.DateTimeFormat
- Intl.NumberFormat
- Iterator
- JSON
- Map
- Math
- NaN
- Number
- Object
- ParallelArray
- Promise
- Proxy
- RangeError
- ReferenceError
- Reflect
- RegExp
- SIMD
- SIMD.float32x4
- SIMD.float64x2
- SIMD.int16x8
- SIMD.int32x4
- SIMD.int8x16
- Set
- StopIteration
- String
- Symbol
- SyntaxError
- TypeError
- TypedArray
- URIError
- Uint16Array
- Uint32Array
- Uint8Array
- Uint8ClampedArray
- WeakMap
- WeakSet
- decodeURI()
- decodeURIComponent()
- encodeURI()
- encodeURIComponent()
- escape()
- eval()
- isFinite()
- isNaN()
- null
- parseFloat()
- parseInt()
- undefined
- unescape()
- uneval()
- Expressions & operators
- 演算子
- 算術演算子
- Array comprehensions
- 代入演算子
- ビット演算子
- カンマ演算子
- 比較演算子
- 条件演算子
- Destructuring assignment
- Expression closures
- Generator comprehensions
- Grouping operator
- Legacy generator function expression
- 論理演算子
- Object initializer
- 演算子の優先順位
- メンバー演算子
- Spread operator
- class 式
- delete
- function
- function* expression
- in
- instanceof
- new
- super
- this
- typeof 演算子
- void 演算子
- yield
- yield*
- Statements & declarations
- Functions
- Classes
- Misc
- New in JavaScript
- JavaScript の新機能
- Mozilla における ECMAScript 5 のサポート
- Mozilla における ECMAScript 6 のサポート
- Mozilla における ECMAScript 7 のサポート
- Firefox JavaScript changelog [Translate]
- JavaScript 1.1 の新機能
- New in JavaScript 1.2 [Translate]
- JavaScript 1.3 の新機能
- JavaScript 1.4 の新機能
- JavaScript 1.5 の新機能
- JavaScript 1.6 の新機能
- JavaScript 1.7 の新機能
- JavaScript 1.8 の新機能
- JavaScript 1.8.1 の新機能
- JavaScript 1.8.5 の新機能
- Documentation:
- Useful lists
- Contribute
The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.
Syntax
switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
expression- An expression matched against each case clause.
case valueN- A case clause used to match against
expression. statementsN- Statements that are executed if
expressionmatches the associated case clause. statements_def- Statements that are executed if
expressiondoes not match any case clause.
説明
もし一致するものが見つかれば、プログラムは関連付けられた文を実行します。もし複数の case が与えられた値に一致するなら、たとえ case がお互いに等しくなくても、一致した最初の case が選択されます。
プログラムは最初に式の値と一致するラベルを持つ case 節を探し、そしてその節に制御を移し、関連付けられた文を実行します。もし一致するラベルが見つからなければ、プログラムは省略可能な default 節を探します。そしてもし見つかれば、その節に制御を移し、関連付けられた文を実行します。もし default 節が見つからなければ、プログラムは switch の終わりに続く文から実行を継続します。規約により、default 節は最後の節ですが、そうである必要はありません。
各ケースのラベルに関連付けられた省略可能な break 文は、一度一致した文が実行されたら、プログラムが switch から抜け出し、switch に続く文から実行を継続することを保証します。もし break が省略されたなら、プログラムは switch 文の中の次の文から実行を継続します。
例
例: switch を使う
次の例では、もし expression が "Bananas" に評価されるなら、プログラムは case "Bananas" で値に一致し、関連付けられた文を実行します。break と遭遇したときは、プログラムは switch から抜け出し、switch に続く文を実行します。もし break が省略されたなら、case "Cherries" に対する文も実行されます。
switch (expr) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
break;
default:
console.log("Sorry, we are out of " + expr);
}
console.log("Is there anything else you'd like?");

