switch in Javascript
By: Syed Fazal in Javascript Tutorials on 2008-08-16
The cousin of the if statement, the switch statement, allows a developer to provide a series of cases for an expression. The syntax for the switch statement is:
switch ( expression ) {
case value : statement
break ;
case value : statement
break ;
case value : statement
break ;
...
case value : statement
break ;
}
Each case says “if expression is equal to value , execute statement ”. The break keyword causes code execution to jump out of the switch statement. Without the break keyword, code execution falls through the original case into the following one.
The default keyword indicates what is to be done if the expression does not evaluate to one of the cases (in effect, it is an else statement). Essentially, the switch statement prevents a developer from having to write something like this:
if (i == 25)
alert(“25”);
else if (i == 35)
alert(“35”);
else if (i == 45)
alert(“45”);
else
alert(“Other”);
The equivalent switch statement is:
switch (i) {
case 25: alert(“25”);
break;
case 35: alert(“35”);
break;
case 45: alert(“45”);
break;
default: alert(“Other”);
}
Two big differences exist between the switch statement in JavaScript and Java. In Javascript, the switch statement can be used on strings, and it can indicate case by nonconstant values:
var BLUE = “blue”, RED = “red”, GREEN = “green”;
switch (sColor) {
case BLUE: alert(“Blue”);
break;
case RED: alert(“Red”);
break;
case GREEN: alert(“Green”);
break;
default: alert(“Other”);
}
Here, the switch statement is used on the string sColor , whereas the case s are indicated by using the variables BLUE , RED , and GREEN , which is completely valid in JavaScript.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments