JavaScript Switch Instructions


JavaScript Switch Instructions

This instruction allows us to test various values of the same variable.
In this way we can do test of various simpler variable value than using if instructions.

The syntax is as follows:


switch (variable) {
case value1:
block of instruction;
break;

case value2:
block of instruction;
break;

case valueX:
block of instruction;
break;

default:
block of instruction;
break;
}

The word in parentheses after the word switch specifies the name variable to test in the case of different values. By the time value of variables to test the same as the value of a case then the block instructions below will be executed. The word “break” means the cessation or break out of the structure of the switch. The word “default” means the block underneath the instructions to be executed when the value of the incoming variable is not equal in all cases that exist.

0 comments:

Post a Comment