January 2003
Beginner to intermediate
1200 pages
23h 42m
English
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; ...