SERVICES

CRYSTAL COMPUTER CONCEPTS

TAXES Fuzzy-Wuzzy device TECHNOLOGY

DELAWARE'S   ·      MIDDLETOWN  ·     ODESSA  ·    TOWNSEND AREA

This statement is comparable to the "Select Case" Branch expression in Visual Basic Script

The JavaScript Switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax

switch(n)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

This is how it works: First we have a single expression n (most often a variable), that is evaluated once.

  1. The value of the expression is then compared with the values for each case in the structure.
  2. If there is a match, the block of code associated with that case is executed.
  3. Use break to prevent the code from running into the next case automatically. For example:


<script type="text/javascript">
var d=new Date();
var theDay=d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm really looking forward to this weekend!");
}
</script>

Return to Table of Contents


DELAWARE'S MIDDLETOWN ODESSA AND TOWNSEND AREA