The switch Statement
case Clause
default Clause



Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site




Review of Terms Related to This Topic





Definition and Purpose

The switch statement provides the means for constructing multiple branching conditions. A typical switch statement contains the switch and case keywords and the optional default keyword and break statements. Each branch condition is expressed with the case clause. The case clause contains the keyword case and the case value where both together are expressed as a label. The switch statement contains an expression that is evaluated and each case value is checked for a match to the expression. Here are the switch statement matching rules:

The switch statement is one of JavaScript's program control statements. This statement and it's supporting keywords will give the programmer the ability to control program flow with the decision making qualities inherent in the statement.





Statement Syntax


switch (expression) { case value1: statement(s)    break; // the break statement causes an immediate exit from the switch structure case value2: statement(s)    break;     ...... case valuen: statement(s)    break; default: statement(s); }



More on Syntax and Statement Behavior





switch Statements and Block Statements

All statements within the switch statement structure (encompassing the case clauses and the default clause) are enclosed with curly braces. This is a use of the statement block. The switch statement is just one example of where the block statement may be used.





The case Clause





The default Clause





ECMA-262 Section 12.11

The switch statement is an ECMAScript core statement and is detailed is section 12.11 of the ECMAScript Language Specification.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson