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

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.
主站蜘蛛池模板: 揭西县| 油尖旺区| 崇左市| 哈巴河县| 罗甸县| 思南县| 桐柏县| 安徽省| 昭通市| 石泉县| 六枝特区| 阜宁县| 红河县| 龙江县| 若尔盖县| 含山县| 沙坪坝区| 临安市| 赫章县| 石狮市| 宜宾市| 侯马市| 曲阜市| 临泽县| 德清县| 巴塘县| 和龙市| 宁乡县| 桃源县| 阿克陶县| 吉水县| 白朗县| 海兴县| 子长县| 瑞金市| 洪湖市| 娄底市| 拉孜县| 全南县| 巴马| 哈密市|