Latest web development tutorials

JavaScript switch statement

JavaScript Statements Reference Manual JavaScript Statements Reference Manual

Examples

Display today week name (Sunday = 0, Monday = 1, Tuesday = 2, ...):

var day;
switch (new Date (). getDay ()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}

day output is:

var d = new Date () getDay (); switch (d) {case 0:. x = "Sunday"; break; case 1: x = "Monday"; break; case 2: x = "Tuesday"; break; case 3: x = "Wednesday"; break; case 4: x = "Thursday"; break; case 5: x = "Friday"; break; case 6: x = "Saturday"; break;} document.write (x );

try it"

The bottom of the article contains more examples.


Definition and Usage

switch statement based on different criteria to perform different actions.

JavaScript switch statement is part of a conditional statement is used to perform different actions based on different conditions. Use switch to select an executive from a plurality of statement blocks.

switch statement evaluates each expression. Then values ​​and values ​​for each case in the structure of the expression for comparison. If there is a match, the block of code associated with that case is executed.

switch statement is usually used in conjunction with default or break keyword. Both are optional:

break keyword is used out of the switch code block. Switch will terminate the execution of the code block. If you omit this keyword, the next block of code swith statement will be executed.

default keyword matching provisions do not exist. default keyword can only appear once in a switch statement. Although it is optional but is recommended that you use this parameter, not what we expect in the case, you can use it to output information.


Browser Support

Statements
switch Yes Yes Yes Yes Yes


grammar

switch (expression) {
case n:
Code block
break;
case n:
Code block
break;
default:
The default code block
}

Parameter Value

parameter description
expression have to. Specified expression calculation. Expression evaluated only once. Expression values ​​and value structure in each case the comparison. If there is a match, the block of code associated with that case is executed.

technical details

JavaScript version: 1.2


Examples s

More examples

Examples

If today is not Saturday, not a Sunday, then the default output information:

var text;
switch (new Date (). getDay ()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}

text output is:


try it"

Examples

Sometimes you want through different use the same code. Or a common set of default values.

Note that in the water, case used a common code, default statement is not in the rearmost swith statement:

var text;
switch (new Date (). getDay ()) {
case 1:
case 2:
case 3:
default:
text = "Looking forward to the Weekend";
break;
case 4:
case 5:
text = "Soon it is Weekend";
break;
case 0:
case 6:
text = "It is Weekend";
}

try it"

Examples

Use the switch statement is executed to determine user input:

var text;
var favDrink = prompt ( "What is your favorite cocktail drink?");
switch (favDrink) {
case "Martini":
text = "! best choice Martini is good for your soul.";
break;
case "Daiquiri":
text = "Daiquiri is my favorite!";
break;
case "Cosmopolitan":
text = "? Really, are you sure Cosmopolitan is your favorite?";
break;
default:
text = "I do not like ......";
break;
}

try it"


Related Pages

JavaScript Tutorials: JavaScript the If ... Else statement

JavaScript Tutorials: JavaScript statements of Switch

JavaScript Reference Manual: JavaScript IF / the else statement


JavaScript Statements Reference Manual JavaScript Statements Reference Manual