Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 121999 of switch

  • Revision slug: JavaScript/Reference/Statements/switch
  • Revision title: switch
  • Revision id: 121999
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[ja:Core JavaScript 1.5 Reference:Statements:switch]] <<langbot>>

Revision Content

Summary

Evaluates an expression, matching the expression's value to a case label, and executes statements associated with that case.

Statement
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262, Edition 3

Syntax

switch (expression) {
   case label1:
      statements1
      [break;]
   case label2:
      statements2
      [break;]
   ...
   case labelN:
      statementsN
      [break;]
   default:
      statements_def
      [break;]
}

Parameters

expression 
An expression matched against each label.
labelN 
Identifier used to match against expression.
statementsN 
Statements that are executed if expression matches the associated label.
statements_def 
Statements that are executed if expression does not match any label.

Description

If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.

The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label 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. By convention, the default clause is the last clause, but it does not need to be so.

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.

Examples

Example: Using switch

In the following example, if expression 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.

switch (expr) {
   case "Oranges":
      document.write("Oranges are $0.59 a pound.<br>");
      break;
   case "Apples":
      document.write("Apples are $0.32 a pound.<br>");
      break;
   case "Bananas":
      document.write("Bananas are $0.48 a pound.<br>");
      break;
   case "Cherries":
      document.write("Cherries are $3.00 a pound.<br>");
      break;
   case "Mangoes":
   case "Papayas":
      document.write("Mangoes and papayas are $2.79 a pound.<br>");
      break;
   default:
      document.write("Sorry, we are out of " + expr + ".<br>");
}
document.write("Is there anything else you'd like?<br>");


{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/switch", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/switch", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/switch" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Evaluates an expression, matching the expression's value to a case label, and executes statements associated with that case.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Statement</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES 3.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262, Edition 3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">switch (<i>expression</i>) {
   case <i>label1</i>:
      <i>statements1</i>
      [break;]
   case <i>label2</i>:
      <i>statements2</i>
      [break;]
   ...
   case <i>labelN</i>:
      <i>statementsN</i>
      [break;]
   default:
      <i>statements_def</i>
      [break;]
}
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>expression</code> </dt><dd> An expression matched against each label.
</dd></dl>
<dl><dt> <code>labelN</code> </dt><dd> Identifier used to match against <code>expression</code>.
</dd></dl>
<dl><dt> <code>statementsN</code> </dt><dd> Statements that are executed if <code>expression</code> matches the associated label.
</dd></dl>
<dl><dt> <code>statements_def</code> </dt><dd> Statements that are executed if <code>expression</code> does not match any label.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.
</p><p>The program first looks for a <code>case</code> clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional <code>default</code> clause, and if found, transfers control to that clause, executing the associated statements. If no <code>default</code> clause is found, the program continues execution at the statement following the end of <code>switch</code>. By convention, the <code>default</code> clause is the last clause, but it does not need to be so.
</p><p>The optional <code><a href="en/Core_JavaScript_1.5_Reference/Statements/break">break</a></code> 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 <code>break</code> is omitted, the program continues execution at the next statement in the <code>switch</code> statement.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_switch"> Example: Using <code>switch</code> </h4>
<p>In the following example, if <code>expression</code> evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When <code>break</code> is encountered, the program breaks out of <code>switch</code> and executes the statement following <code>switch</code>. If <code>break</code> were omitted, the statement for case "Cherries" would also be executed.
</p>
<pre>switch (expr) {
   case "Oranges":
      document.write("Oranges are $0.59 a pound.&lt;br&gt;");
      break;
   case "Apples":
      document.write("Apples are $0.32 a pound.&lt;br&gt;");
      break;
   case "Bananas":
      document.write("Bananas are $0.48 a pound.&lt;br&gt;");
      break;
   case "Cherries":
      document.write("Cherries are $3.00 a pound.&lt;br&gt;");
      break;
   case "Mangoes":
   case "Papayas":
      document.write("Mangoes and papayas are $2.79 a pound.&lt;br&gt;");
      break;
   default:
      document.write("Sorry, we are out of " + expr + ".&lt;br&gt;");
}
document.write("Is there anything else you'd like?&lt;br&gt;");
</pre>
<p><br>
</p>
<div class="noinclude">
</div>
{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/switch", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/switch", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/switch" } ) }}
Revert to this revision