合乎逻辑的&&和|| JavaScript中的运算符 [英] The logical && and || operators in JavaScript

查看:111
本文介绍了合乎逻辑的&&和|| JavaScript中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进一步澄清一些事情。

I wanted further clarification on something.

考虑一下:

var a = 42;
var b = "abc";
var c = null;

a || b;     // 42 
a && b;     // "abc"
c || b;     // "abc" 
c && b;     // null 

我知道 || 运算符,如果对第一个操作数的测试为真, || 表达式将得到第一个操作数(a或c)的值。如果测试为假,则 || 表达式将生成第二个操作数(b)的值。

I know that for the || operator, if the test on the first operand is true, the || expression results in the value of the first operand (a or c). If the test is false, the || expression results in the value of the second operand (b).

相反,对于&& 运算符,如果测试为真,&& 表达式导致第二个操作数(b)的值。如果测试结果为false,则&& 表达式会产生第一个操作数的值(a或c)

Inversely, for the && operator, if the test is true, the && expression results in the value of the second operand (b). If the test is false, the && expression results in the value of the first operand (a or c)

那么当您使用带链接值的&& || 运算符时,到底发生了什么? :

So what exactly is happening when you use the && and || operators with chaining values like:

if(a && b && c && d && e){
    //do something;
}

if(a || b || c || d || e){
    //do something
}

链接值时究竟发生了什么?因为在第一个例子中(涉及​​&& 运算符)如果a为真,那么应该返回b吗?那么在这一点上是否考虑过c或d?

What exactly is taking place when you chain the values? Because in the first example (involving the && operator)if a is true, then b should be returned right? so are c or d even taken into account at that point?

推荐答案


那么当你使用&&时到底发生了什么? ; || 具有链接值的运算符

So what exactly is happening when you use the && and || operators with chaining values

&& 是一个二元运算符,带有一个左操作数和一个右操作数。表达式 a&& b&& c&& d&& e 基于进行解析关联性规则如下所示:

&& is a binary operator, with one left-hand operand and one right-hand operand. The expression a && b && c && d && e is parsed based on associativity rules so that it looks like this:

if (a && (b && (c && (d && e)))) {

基于语法&&& ,如果 a 是假的,整个条件立即计算为一个,这是假的。如果 a 是真实的,则表达式评估为右侧,即 b&& (c&&(d& e)))。在这种情况下,如果 b 是假的,那么该子表达式,以及整个表达式,立即计算为 b 这是假的;如果 b 是真实的,那么该子表达式的评估结果为右侧,即 c&& (d&& e),并且该过程继续。

Based on the semantics of &&, if a is falsy, the entire condition immediately evaluates to a, which is falsy. If a is truthy, then the expression evaluates to the right side, which is b && (c && (d && e))). In that case if b is falsy, then that sub-expression, and thus the entire expression, immediately evaluates to b, which is falsy; if b is truthy, then that sub-expression evaluates to its right side, which is c && (d && e), and the process continues.

最终结果是整个表达式的直观行为falsy,因此对于如果分支到,它就足以让任何变量变得虚假。一旦遇到任何假变量,评估将短路 - 导致麻痹。因为整个表达式是真实的,因此对于 if 分支,所有变量必须是真实的。

The net result is the intuitive behavior that for the entire expression to be falsy, and therefore for the if branch to not be taken, it suffices for any of the variables to be falsy. The evaluation will "short-circuit"--resulting in falsy--as soon as any falsy variable is encountered. For the entire expression to be truthy, and therefore for the if branch to be taken, all the variables must be truthy.

对于 || ,逻辑相反。因为整个表达式都是真实的,因此对于 if 分支,它足以让任何变量都变得真实。只要遇到第一个真值,评估就会短路 - 导致真相。因为整个表达式是假的,因此对于如果分支,所有变量都必须是假的。

For ||, the logic is reversed. For the entire expression to be truthy, and therefore for the if branch to be taken, it suffices for any of the variables to be truthy. The evaluation will "short-circuit"--resulting in truthy--as soon as the first truthy value is encountered. For the entire expression to be falsy, and therefore for the if branch not to be taken, all the variables must be falsy.

你不知道JS 也有很好的解释。

这篇关于合乎逻辑的&&和|| JavaScript中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆