Multiple Identical Options in Switch Statements

Multiple Identical Options in Switch Statements

HighTech6839v

54 года назад

27 Просмотров

JavaScript Switch Statement - The switch statement is used to perform different actions based on different conditions. Use the switch statement to select one of many code blocks to be executed.

The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If there is no match, the default code block is executed.

The break Keyword - When JavaScript reaches a break keyword, it breaks out of the switch block. This will stop the execution inside the switch block. It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.

The default Keyword - The default keyword specifies the code to run if there is no case match. The default case does not have to be the last case in a switch block.

Nested down below is the code for this lesson:

function sequentialSizes(val) {
var answer = "";
switch (val) {
case 1:
case 2:
case 3:
answer = "Low";
break;
case 4:
case 5:
case 6:
answer = "Mid";
break;
case 7:
case 8:
case 9:
answer = "High";
}
return answer;
}
Ссылки и html тэги не поддерживаются


Комментарии: