两条路径分散在树林中:使用JavaScript if ... else语句

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 . 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 并且 condition1false ,则将运行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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值