switch

JavaScript1.2+, JScript 3.0+ NES3+, Nav4+, IE4+ Syntax

switch(expression){
  case label1:
							code;
    break;
  case label2:
							code;
    break;
  case labelN:
							code;
    break;
  default:
    code;
}

Description

The switch statement allows you to process the expression passed by matching it with a label—from label1 to labelN. If there is a match, the code following that label is executed. If the expression passed does not match a label, the default section is executed. Note that you can have as many labels as you deem necessary in your script.

Example

Listing 6.262 has a text field and a button. Users are asked to enter a day of the week into the field. When they press the button, a function is called that contains a switch, which verifies they entered a correct ...

Get Pure JavaScript 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.