switch/case

switch / case is an interesting statement, and is very useful. It is used to 'switch' between several given 'cases' within your script. It is something like a shortcut, providing execution paths for the conditions of an expression. Examine the syntax and example, given below.

syntax:

switch (expression) {

case caseName:

statements

break;

case caseName:

statements

break;

default:

statements

EXAMPLE

var inventoryList = document.form[0].itemList

var neededItemList = inventoryList.options[inventoryList. selectedIndex].value

switch (neededItemList) {

case "neededItemOne":

document.forms[0].serialNumber.value = "1386-4854"

break;

case "neededItemTwo":

document.forms[0].serialNumber.value = "1386-8512"

break;

default:

document.forms[0].serialNumber.value = "No Items Needed"

}

The above example shows a switch / case statement with two 'cases'. If the first case, 'neededItemOne' is selected by the conditions of 'neeedItemList', the value returned is the serial number, '1386-4854'. If the second case is selected, the value returned is the second serial number. If neither cases satisfy the conditions of 'neededItemList', then the default value is returned, which is 'No Items Needed'.