The Switch statement is a convenient way of handling
situations where you need to handle many actions based on the value of a
variable. The Switch statement starts of with the switch statement which
contains an expression that should evaluate into a number. After the switch
statement is a block of code which consists of case statements and an optional
default statement.
switch (number)
{
case 1:
console.log("The
number is one");
break;
case 2:
console.log("The
number is two");
break;
case 3:
console.log("The
number is three");
break;
default:
console.log("The
number is not one, two or three");
}
After each of the case statements is the code you want to
execute if the variable being switched equals the value of the case. The code
should end with a break statement, which causes the program to skip over the
rest of the switch block. If there is no break statement, all code in the
following case statement will also be executed. In some cases, this is exactly
what you want to happen, as it allows you to define a group of numbers that all
do the same action. Forgetting the break statement is a very common mistake for
beginners to make and is even something that experts have made resulting in
devastating results.
The default statement will be executed if none of the case
statements match the value of the switch expression. No break statement is
needed after the default code, though I usually do have a break statement out
of habit. While the default statement is optional, it is a good habit to always
have a default action, even if the default is simply a trace statement telling
you that you have reached code that should not be reached.
The switch statement we have above could be done with if
statements as you can see below.
if (number == 1) {
console.log("The number is one");
} else if (number == 2) {
console.log("The number is two");
} else if (number == 3) {
console.log("The number is three");
} else {
console.log("The
number is not one, two or three");
}
The choice between using switch and if statements is more of
a cosmetic one with JavaScript. Switch statements are a convenience statement
designed to replace large numbers of if then else statements in a more readable
way.
The switch statement comes directly from the C programming
language. While I am a fan of that language, being one of the first programming
languages that I learned, I have been bitten by the switch statement too many
times and have uncovered bugs in other people’s code due to the forgetting of a
break statement frequently. Therefore, I am happy that new languages are
replacing the default fall-through behavior with default break behavior
requiring a statement to fall through.
Another common bug with the switch statement is not having a
default block when a case does not cover all possibilities. The result of not
having a default is essentially a default that does nothing which may be what
you want. It could also be an indication that you expect only the values in the
case to exist. Having a default that reports an error if it is reached is
recommended in this situation.
No comments:
Post a Comment