O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating switch Statements

The JavaScript switch statement is the next step up in decision making after the if statement. You use the switch statement if you want to test a large number of cases and don't want to construct a long ladder of if...else statements.

Here's how it works: You compare a test expression against a number of values. If one of those values matches, the code associated with the value is executed until the JavaScript interpreter finds a break statement. Here's what the switch statement looks like in outline:

switch(test){
    case value1:
          .
          .
          .
        code executed if test matches value1
          .
          .
          .
        break;
    case value2:
          .
          .
          .
        code executed if test matches value2
          .
          .
          .
        break;
    case value3:
          .
          .
          .
        code executed if test matches value3 . . . break; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required