了解JavaScript逻辑运算符

Logical operators are most commonly used in JavaScript where the value of evaluated expressions are shared to come to a decision; these expressions may also be evaluated as not true, or in an either-or condition. As such, they’re often associated with if statements.

逻辑运算符最常用于JavaScript ,在JavaScript中共享评估表达式的值来做出决定; 这些表达式也可能被评估为正确, 或者处于“或”或“或”状态。 因此,它们通常与if语句关联。

逻辑与:&& (Logical AND: &&)

Compares two conditions and returns a true or false summary. In the context of an if:

比较两个条件并返回truefalse摘要。 在if的上下文中:

var x = 5;
var y = "Monday";
if (x > 3 && y == "Monday") {
    console.log("Success!");
}

The expressions are evaluated from left to right; both the expressions must return true in order for the Success! statement to be executed. If either is false, the if will be ignored, and “Success!” will never be written to the console. That is:

表达式从左到右求值; 为了Success! 两个表达式都必须返回true Success! 要执行的语句。 如果其中一个为false ,则if将被忽略,并“成功!” 永远不会写入控制台 。 那是:

Logical AND Evaluations
LeftRightResult
truetrueTRUE
truefalseFALSE
falsetrueFALSE
falsefalseFALSE
逻辑与评估
剩下 结果
真正 真正 真正
真正
真正

Of course, it’s entirely possible to have more than two conditions. As they grow in number and complexity, it’s advisable to use parentheses to clarify the evaluations:

当然,完全可以有两个以上的条件。 随着它们的数量和复杂性的增加,建议使用括号来阐明评估结果:

if ((x > 3) && (y == "Monday") && (year < 2016)) {
    … code …
}

A common mistake is to use a single ampersand for a logical AND, which doesn’t work; like equality operators, it’s advisable to remember that you are comparing values, and thus need double symbols.

一个常见的错误是对逻辑AND使用单个&符,这是行不通的。 像相等运算符一样 ,建议记住您正在比较值,因此需要双符号。

逻辑或:|| (Logical OR: ||)

In an OR comparison, any evaluation that is true will trigger conditional code:

OR比较中, 任何为true的评估都将触发条件代码:

var a = 16;
var b = "Slpadash";
if (a > 3 || b == "Exemplary") {
    console.log("Success!");
}

In the example above, the second evaluation will never be true, but the first one will be. Because an OR has been used, the console code will be run.

在上面的示例中,第二个评估永远不会是正确的,但第一个将是。 因为使用了OR ,所以将运行console代码。

逻辑非:! (Logical NOT: !)

Works as a “not” in a comparison statement:

在比较语句中用作“ not”:

var c = true;
var d = 1979;
if (c == true && d !== 1980) {
    console.log("Correct");
}

In the code above, Correct will be printed out. The comparison would read as “if c is true and d is not equal to 1980…”

在上面的代码中,将打印出Correct 。 比较结果为“如果ctrued不等于1980…”

! can also be used by itself:

! 也可以单独使用:

var c = false;
var d = 1979;
if (!c || d <> 1980) {
    console.log("Correct");
}

The comparison would be read as “if c is false or d is greater or less than 1980”.

比较将被解读为“如果c为假或d大于或小于1980”。

结论 (Conclusion)

Logical operators are key to making complex decisions in JavaScript, and can be used in many different coding patterns, including ternary operators, which we’ll be looking at next.

逻辑运算符是在JavaScript中做出复杂决策的关键,并且可以用在许多不同的编码模式中,包括三元运算符,我们将在后面讨论。

翻译自: https://thenewcode.com/362/Understanding-JavaScript-Logical-Operators

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值