The switch Statement
The switch statement enables you to execute one or more statements when a specified expression's value matches a label.
The script takes on the following layout:
switch (expression) { case label1: statementlist case label2: statementlist }
In the following example, the value inputted into the prompt box determines which statements are run (the before and after are shown in Figures 7.11 and 7.12):
<script language="JavaScript"> <!-- Cloaking device on! var yourchoice; yourchoice = prompt("Choose a number between 1 and 4", "1, 2, 3 or 4") switch (yourchoice) { case "1": alert("You typed in a 1"); break; case "2": alert("You typed in a 2"); break; case "3": alert("You typed in a 3"); break; case "4": alert("You typed in a ...
Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.