Javascript switch Statement

Introduction

The syntax for the switch statement in Javascript:

switch (expression) {
    case value1://from   www.  j  a v  a2 s  .c o m
        statement
        break;
    case value2:
        statement
        break;
    case value3:
        statement
        break;
    case value4:
        statement
        break;
    default:
        statement
}

In each case in a switch statement, if the expression is equal to the value, execute the 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) {/*from  w ww  .  ja  va 2 s . c  o  m*/
    console.log("25");
} else if (i == 35) {
    console.log("35");
} else if (i == 45) {
    console.log("45");
} else {
    console.log("Other");
}

The equivalent switch statement is as follows: 
switch (i) {/*ww  w.  j  av a  2s . c o m*/
    case 25:
        console.log("25");
        break;
    case 35:
        console.log("35");
        break;
    case 45:
        console.log("45");
        break;
    default:
        console.log("Other");
}

It's best to always put a break statement after each case to avoid having cases fall through into the next one.

If you need a case statement to fall through, include a comment indicating that the omission of the break statement is intentional:

switch (i) {//from   w w w. jav a2  s .  co m
    case 25:
        /* falls through */
    case 35:
        console.log("25 or 35");
        break;
    case 45:
        console.log("45");
        break;
    default:
        console.log("Other");
}

Javascript switch statement works with all data types.

We can use strings and even objects.

The case values need not be constants. They can be variables and even expressions.

switch ("hello world") {
    case "hello" + " world":
        console.log("Greeting was found.");
        break;/*www  .j a v  a2  s.  c o  m*/
    case "goodbye":
        console.log("Closing was found.");
        break;
    default:
        console.log("Unexpected message was found.");
}

In this example, a string value is used in a switch statement.

The first case is actually an expression that evaluates a string concatenation.

We can have case expressions to do things like this:

let num = 25;/* w ww  .j a  v a2 s  .  c om*/
switch (true) {
    case num < 0:
        console.log("Less than 0.");
        break;
    case num >= 0 && num <= 10:
        console.log("Between 0 and 10.");
        break;
    case num > 10 && num <= 20:
        console.log("Between 10 and 20.");
        break;
    default:
        console.log("More than 20.");
}

The switch statement compares values using the identically equal operator, so no type coercion occurs.




PreviousNext

Related