A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/statements/switch below:

switch · WebPlatform Docs

switch Summary

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
}
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.
Examples

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.");
     }
 }

 
 

 
 

 
 

 
 

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.");
     }
 }

 
 

 
 

 
 

 
 
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:

See also Other articles Attributions

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4