- Object-Oriented JavaScript(Second Edition)
- Stoyan Stefanov Kumar Chetan Sharma
- 496字
- 2021-08-13 16:19:29
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 theswitch
statement. If the result of the comparison istrue
, the code that follows the colon after the case is executed. - There is an optional
break
statement to signal the end of thecase
block. If thisbreak
statement is reached, theswitch
is all done. Otherwise, if thebreak
is missing, the program execution enters the nextcase
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 totrue
.
In other words, the step-by-step procedure for executing a switch
statement is as follows:
- Evaluate the
switch
expression found in parentheses; remember it. - Move to the first
case
and compare its value with the one from step 1. - If the comparison in step 2 returns
true
, execute the code in thecase
block. - After the case block is executed, if there's a
break
statement at the end of it, exit the switch. - If there's no
break
or step 2 returnedfalse
, move on to the nextcase
block. - Repeat steps 2 to 5.
- 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 indentcase
from theswitch
, 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 acase
and have two cases sharing the same code. This is fine, but doesn't change the rule that if there's code that follows acase
statement, this code should end with abreak
. In terms of indentation, aligning thebreak
with thecase
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 theswitch
statement, even if none of the cases matches the value being switched.
推薦閱讀
- Designing Hyper-V Solutions
- Learning SQLite for iOS
- 正則表達(dá)式經(jīng)典實(shí)例(第2版)
- Working with Odoo
- ASP.NET Core 2 Fundamentals
- Mastering Business Intelligence with MicroStrategy
- Mastering Xamarin.Forms(Second Edition)
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)
- Vue.js 3應(yīng)用開(kāi)發(fā)與核心源碼解析
- 計(jì)算語(yǔ)言學(xué)導(dǎo)論
- Laravel Design Patterns and Best Practices
- C#網(wǎng)絡(luò)編程高級(jí)篇之網(wǎng)頁(yè)游戲輔助程序設(shè)計(jì)
- Cloud Development andDeployment with CloudBees
- JavaScript前端開(kāi)發(fā)程序設(shè)計(jì)教程(微課版)
- Learning SaltStack(Second Edition)