The switch statement in JavaScript is used in a manner similar to of a series of if / if else statements. The switch statement evaluates a series of conditions, if a match is found, the program executes the following commands. A value is passed to the switch command and enclosed in parentheses. This value is then tested with a number of case clauses.

Following is an example of a switch statement:

switch(pickColor) {
    case "blue":
        // steps to take go here
        break;
    case "red":
        // steps to take go here
        break;
    case "green":
        // steps to take go here
        break;
    case "orange":
        // steps to take go here
        break;
    default:
        // steps to take go here
}

In this example the switch statement goes through a series of case clauses. If the clause is true, then it proceeds with executing the following statements.

The switch statement will continue testing each clause unless a break; is encountered. In that case the switch statement is stopped and the program goes back to what it was doing before. Adding break; is optional. It is not always necessary, but it will shorten the time the program has to run.

The last line is often a default: line. This will happen if none of the other case: clauses were true. This is also optional, but included if necessary.