Javascript Reference
Categories: Operators

javascript Operators switch/case

@July 12, 2008, 5:13 a.m.
switch/case Firefox/Netscape/NN 4 IE 4 ECMA 3  
  

Provides a shortcut to execution paths for numerous conditions of an expression. The optional break statement at the end of each case block shortcuts execution of the switch statement, and also prevents the inadvertent execution of the default block, if present.

 
Example
 
var productList = document.forms[0].prodList;
var chosenItem = productList.options[productList.selectedIndex].value;
switch(chosenItem) {
  case "Small Widget":
    document.forms[0].price.value = "44.95";
    break;
  case "Medium Widget":
    document.forms[0].price.value = "54.95";
    break;
  case "Large Widget":
    document.forms[0].price.value = "64.95";
    break;
  default:
    document.forms[0].price.value = "Nothing Selected";
}

Powered by Linode.