Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript switch Statement

JavaScript switch Statement

JavaScript Statement Reference JavaScript statement reference manual

Example

Display today's day 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 The output is:

Try it »

More examples are included at the bottom of this article.


Definition and Usage

The switch statement is used to perform different actions based on different conditions.

The switch statement is part of JavaScript's conditional statements, used to perform different actions according to different conditions. Use switch to select and execute one of multiple blocks of statements.

The switch statement evaluates each expression. The value of the expression is then compared with the value of each case in the structure. If a match exists, the block of code associated with that case is executed.

The switch statement is usually used together with the break or default keywords. Both are optional:

break The keyword is used to break out of the switch block. It terminates the execution of the switch block. If this keyword is ignored, the next block of the swith statement is executed.

default The keyword is used to specify what to do when no match exists. The default keyword can appear only once in a switch statement. Although it is an optional parameter, it is recommended to use it to output information in unexpected cases.


Browser support

Statement
switch Yes Yes Yes Yes Yes

Syntax

switch(

expression

) {

    case

n

:

        Code block

        break;

    case

n

:

        Code block

        break;

    default:

        Default block

}

Parameter value

Parameters Description
expression Required. Specifies the expression to be evaluated. The expression is evaluated only once. The value of the expression is compared with the value of each case in the structure. If a match exists, the block of code associated with that case is executed.

Technical Details

JavaScript version: 1.2

More examples

Example

If today is neither Saturday nor Sunday, output the default message:

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 The output result is:

Try it »

Example

Sometimes you want to use the same code for different cases. Or set a common default value.

Note that in 3, the case uses common code, and the default statement is not at the end of the 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 »

Example

Use the switch statement to evaluate the user's input:

var

text

;

var

favDrink

=

prompt

(

"

What is your favorite cocktail drink?

"

)

;

switch

(

favDrink

)

{

case

"

Martini

"

:

text

=

"

Excellent choice! Martini is good for your soul.

"

;

break

;

case

"

Daiquiri

"

:

text

=

"

Daiquiri is also my favorite!

"

;

break

;

case

"

Cosmopolitan

"

:

text

=

"

Really? Are you sure Cosmopolitan is your favorite?

"

;

break

;

default

:

text

=

"

I have no favorites...

"

;

break

;

}

Try it »


Related pages

JavaScript tutorial: JavaScript If...Else Statements

JavaScript tutorial: JavaScript Switch Statements

JavaScript Reference Manual: JavaScript if/else Statements


JavaScript Statement Reference JavaScript statement reference manual