官术网_书友最值得收藏!

Switch

If you find yourself using an if condition and having too many else if parts, you could consider changing the if to a switch:

var a = '1',
    result = '';
switch (a) {
case 1:
  result = 'Number 1';
  break;
case '1':
  result = 'String 1';
  break;
default:
  result = 'I don\'t know';
  break;
}

The result after executing this is "String 1". Let's see what the parts of a switch are:

  • The switch statement.
  • An expression in parentheses. The expression most often contains a variable, but can be anything that returns a value.
  • A number of case blocks enclosed in curly brackets.
  • Each case statement is followed by an expression. The result of the expression is compared to the expression found after the switch statement. If the result of the comparison is true, the code that follows the colon after the case is executed.
  • There is an optional break statement to signal the end of the case block. If this break statement is reached, the switch is all done. Otherwise, if the break is missing, the program execution enters the next case block.
  • There's an optional default case marked with the default statement and followed by a block of code. The default case is executed if none of the previous cases evaluated to true.

In other words, the step-by-step procedure for executing a switch statement is as follows:

  1. Evaluate the switch expression found in parentheses; remember it.
  2. Move to the first case and compare its value with the one from step 1.
  3. If the comparison in step 2 returns true, execute the code in the case block.
  4. After the case block is executed, if there's a break statement at the end of it, exit the switch.
  5. If there's no break or step 2 returned false, move on to the next case block.
  6. Repeat steps 2 to 5.
  7. If you are still here (no exit in step 4), execute the code following the default statement.
Tip

Best practice tips

  • Indent the code that follows the case lines. You can also indent case from the switch, but that doesn't give you much in terms of readability.
  • Don't forget to break.
  • Sometimes, you may want to omit the break intentionally, but that's rare. It's called a fall-through and should always be documented because it may look like an accidental omission. On the other hand, sometimes you may want to omit the whole code block following a case and have two cases sharing the same code. This is fine, but doesn't change the rule that if there's code that follows a case statement, this code should end with a break. In terms of indentation, aligning the break with the case or with the code inside the case is a personal preference; again, being consistent is what matters.
  • Use the default case. This helps you make sure you always have a meaningful result after the switch statement, even if none of the cases matches the value being switched.
主站蜘蛛池模板: 清远市| 信阳市| 富顺县| 麻阳| 昭苏县| 托克逊县| 和静县| 天水市| 河池市| 望城县| 周宁县| 阿拉善盟| 凌海市| 桃园市| 稻城县| 电白县| 武乡县| 临城县| 会宁县| 祁门县| 扶风县| 兴宁市| 和平区| 淮安市| 宽城| 湘乡市| 定州市| 于都县| 安康市| 农安县| 安岳县| 托克托县| 剑河县| 房产| 东乡县| 灵山县| 元谋县| 类乌齐县| 桃园县| 江油市| 奉贤区|