switch Statement

Enables the execution of one or more statements when a specified expression's value matches a label.

switch (expression) { 
   case label1 :
 [ ...
 [ case labelN :
      [break;] ] ]
 [ default :


  • expression
    Required. The expression to be evaluated.

  • label1, ..., labelN
    Required. An identifier to be matched against expression. If label === expression, execution starts with the statement list immediately after the colon, and continues until it encounters either a break statement, which is optional, or the end of the switch statement.

  • statementlist1, ..., statementlistN, statementlistDefault
    Optional. One or more statements to be executed.


Use the default clause to provide a statement to be executed if none of the label values matches expression. It can appear anywhere within the switch code block.

Zero or more label blocks may be specified. If no label matches the value of expression, and a default case is not supplied, no statements are executed.

Execution flows through a switch statement as follows:

  • Evaluate expression and look at label in order until a match is found.

  • If a label value equals expression, execute its accompanying statement list.

    Continue execution until a break statement is encountered, or the switch statement ends. This means that multiple label blocks are executed if a break statement is not used.

  • If no label equals expression, go to the default case. If there is no default case, go to last step.

  • Continue execution at the statement following the end of the switch code block.


The following ASP.NET example tests an object for its type. In this case, only one type is used, but you should be able to clearly see how the function works with other object types.

<%@ language="jscript" %>
var d = new Number();
function MyObjectType(obj : Object) : String {
   switch (obj.constructor){
      case Date:
         return "Object is a Date.";
      case Number:
         return "Object is a Number.";
      case String:
         return "Object is a String.";
         return "Object is unknown.";


Version 3

See Also


break Statement

if...else Statement