Almost all JavaScript code eventually comes to a decision, a judgement whether or not to take an action. This is usually resolved with an if
statement:
几乎所有JavaScript代码最终都会做出决定,即是否采取行动的判断。 通常使用if
语句解决此问题:
if (condition) { statement(s); }
The condition may be an expression using any valid combination of operators. If the expression evaluates as true, the statement(s) in the curly braces are executed.
条件可以是使用任何有效的运算符组合的表达式 。 如果表达式的计算结果为true ,则执行大括号中的语句。
Technically, curly braces are optional around a single statement; a block statement (i.e. using curly braces) is required only if there are multiple statements. However, for the sake of consistency and readability, it’s recommended to always use curly braces.
从技术上讲,大括号在单个语句周围是可选的; 仅当有多个语句时才需要使用block语句(即使用花括号)。 但是,为了保持一致性和可读性,建议始终使用花括号。
For new coders it’s particularly important to check that an if
condition will resolve to true
or false
:
对于新编码人员,检查if
条件是否可以解析为true
或 false
尤为重要:
if (4 > 5) { console.log("Impossible!"); }
In the statement above, “Impossible!” will never be written to the console, as 4 can never be greater than 5. Similarly:
在上面的声明中,“不可能!” 永远不会写入控制台,因为4永远不能大于5。类似地:
if (4 > 3) { console.log("Noduh"); }
The above statement will always be executed, and should not be in an if
.
上面的语句将始终执行,并且不应在if
。
A common mistake is to use an assignment operation in the condition, which will always be executed:
一个常见的错误是在条件中使用赋值操作 ,该赋值操作将始终执行:
if (var = 5) { statement(s); }
Instead, this should be a strictly equivalent operator:
相反,这应该是一个严格等效的运算符 :
if (var === 5) { statement(s); }
没走的路 (The Path Not Taken)
To take care of alternatives, you can place statements in a else
:
要注意其他选择,可以将语句放在else
:
if (condition1) {
statement1
} else {
statement2
}
If the condition is met, statement1
is executed; if the condition evaluates to false
, statement2
will be executed instead.
如果满足条件,则执行statement1
;否则,执行statement1
。 如果条件评估为false
,那么将改为执行statement2
。
if else
statements can also be written as a shorter ternary operator.
if else
语句也可以写成较短的三元运算符 。
更准确的Elses (More Accurate Elses)
if else
is a “either this or this” decision. We can clarify our forked conditions further by using an else if
construction. For example:
if else
是“这个或这个”决定。 我们可以使用else if
构造进一步阐明我们的分叉条件。 例如:
if (condition1) {
statement1
} else if (condition2) {
statement2
} else if (condition3) {
statement3
} else {
general statement
}
more code…
In the above code, condition1
will be tested first. If it evaluates to true
, statement1
will be executed, and the code will move on more code
. If it is not met, condition2
will be tested, followed by condition3. If either of those evaluate as true
, their respective statements will be run exclusively, and execution will move on to more code
. If both of them test as false
and condition1
is false
, the general statement
will be run.
在上面的代码中,将首先测试condition1
。 如果其结果为true
,则将执行statement1
,并且代码将在more code
上移动。 如果不能满足, condition2
将受到考验,其次是condition3。 如果其中任何一个评估为true
,则它们各自的语句将排他地运行,并且执行将移至more code
。 如果它们都测试为false
并且 condition1
为false
,则将运行general statement
。
Note that the statement is else if
- two separate words. There is no elseif
in JavaScript.
请注意,该语句为else if
两个单独的单词。 JavaScript中没有elseif
。
嵌套决策 (Nested Decisions)
It is also possible to nest if
statements:
也可以嵌套if
语句:
if (condition1) {
statementA;
} else {
if (condition2) {
statementB;
} else {
statementC;
}
}
Functionally, nested if
statements can have the same result as else if
statements: your choice to use one or the other is largely a matter of taste, convenience and coding style.
从功能上讲,嵌套的if
语句可以具有与else if
语句相同的结果:使用一个或另一个else if
语句在很大程度上取决于口味,便利性和编码风格。
Note the tab-indentation pattern of the nested statements: since if else
statements can become very complex, clear presentation in code is very important.
请注意嵌套语句的制表符缩进模式:由于if else
语句会变得非常复杂,因此在代码中清晰呈现非常重要。
结论 (Conclusion)
There are more ways to create branching decisions in JavaScript than if - else
statements, including switch
, which we will be looking at next.
与if - else
语句(包括switch
,在JavaScript中创建分支决策的方法更多,下面将介绍。
Two roads diverged in a yellow wood, And sorry I could not travel both And be one traveler, long I stood And looked down one as far as I could To where it bent in the undergrowth;
两条道路在黄色的树林中分叉,对不起,我无法同时行进,成为一个行人,我站了很久,尽我所能地低头看向灌木丛中的弯曲处。
Then took the other, as just as fair, And having perhaps the better claim, Because it was grassy and wanted wear; Though as for that the passing there Had worn them really about the same,
然后公平地取下另一个,也许有更好的主张,因为它长满了草,想要穿。 虽然那路过的人真的穿了差不多的衣服,
And both that morning equally lay In leaves no step had trodden black. Oh, I kept the first for another day! Yet knowing how way leads on to way, I doubted if I should ever come back.
那天早晨和那天晚上同样躺在地上,没有脚踩过黑色。 哦,我保留了第一天! 然而,我知道路是怎么走的,所以我怀疑我是否应该回来。
I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I— I took the one less traveled by, And that has made all the difference.
我将叹息地告诉这个问题,因为它在某个年代和年代都在变老:两条路在树林中分叉,我-我走过的路少了,这一切都改变了。
The Road Not Taken, Robert Frost
未选择的路 ,罗伯特·弗罗斯特
翻译自: https://thenewcode.com/352/Two-Paths-Diverged-In-A-Wood-Using-JavaScript-if-else-Statements