Enables the execution of one or more statements when a specified expression's value matches a label.
Syntax
switch (expression) {
case label :
statementlist
case label :
default :
statementlist
}
Parameters
expression
The expression to be evaluated.
label
An identifier to be matched against expression. If label is an expression, execution starts with the statementlist immediately after the colon, and continues until it encounters either a break statement, which is optional, or the end of the switch statement.
statementlist
One or more statements to be executed.
Remarks
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
expressionand look atlabelin order until a match is found.If a
labelvalue equalsexpression, execute its accompanyingstatementlist.Continue execution until a
breakstatement is encountered, or theswitchstatement ends. This means that multiplelabelblocks are executed if abreakstatement is not used.If no
labelequalsexpression, go to thedefaultcase. If there is nodefaultcase, go to last step.Continue execution at the statement following the end of the
switchcode block.
Example
The following example tests an object for its type.
function MyObjectType(obj) {
switch (obj.constructor) {
case Date:
document.write("Object is a Date.");
break;
case Number:
document.write("Object is a Number.");
break;
case String:
document.write("Object is a String.");
break;
default:
document.write("Object is unknown.");
}
}
// Output when obj is a Date:
// Object is a Date.
// Output when obj is a Number:
// Object is a Number.
// Output when obj is a String:
// Object is a String.
// Output when obj is something other than a Date, Number, or String:
// Object is unknown.
Example
The following code shows what happens if you do not use a break statement.
function MyObjectType(obj) {
switch (obj.constructor) {
case Date:
document.write("Object is a Date.");
case Number:
document.write("Object is a Number.");
case String:
document.write("Object is a String.");
default:
document.write("Object is unknown.");
}
}
// Output when obj is a Date:
// Object is a Date.Object is a Number.Object is a String.Object is unknown.
// Output when obj is a Number:
// Object is a Number.Object is a String.Object is unknown.
// Output when obj is a String:
// Object is a String.Object is unknown.
// Output when obj is something other than a Date, Number, or String:
// Object is unknown.
Requirements
Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

