简体   繁体   English

逻辑 && 和 || JavaScript 中的运算符

[英]The logical && and || operators in JavaScript

I wanted further clarification on something.我想进一步澄清一些事情。

Consider this:考虑一下:

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

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

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).表达式产生第一个操作数(a 或 c)的值。 If the test is false, the ||如果测试为假,则|| expression results in the value of the second operand (b).表达式产生第二个操作数 (b) 的值。

Inversely, for the && operator, if the test is true, the && expression results in the value of the second operand (b).相反,对于&&运算符,如果测试为真,则&&表达式会产生第二个操作数 (b) 的值。 If the test is false, the && expression results in the value of the first operand (a or c)如果测试结果为 false, &&表达式将得出第一个操作数(a 或 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
}

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?因为在第一个示例中(涉及&&运算符)如果 a 为真,那么应该返回 b 对吗? so are c or d even taken into account at that point?那么 c 或 d 甚至在那时被考虑在内吗?

So what exactly is happening when you use the && and ||那么当你使用&&||时到底发生了什么operators with chaining values具有链接值的运算符

&& 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:表达式a && b && c && d && e基于关联规则进行解析,使其看起来像这样:

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

Based on the semantics of && , if a is falsy, the entire condition immediately evaluates to a , which is falsy.根据&&的语义,如果a为假,则整个条件立即计算为a ,这是假的。 If a is truthy, then the expression evaluates to the right side, which is b && (c && (d && e))) .如果a为真,则表达式的计算结果为右侧,即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;在这种情况下,如果b为假,那么该子表达式以及整个表达式立即计算为b ,即为假; if b is truthy, then that sub-expression evaluates to its right side, which is c && (d && e) , and the process continues.如果b为真,则该子表达式计算其右侧,即c && (d && e) ,然后过程继续。

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.净结果是直观的行为,对于整个表达式是falsy,因此对于if分支被采取,就足够了任何变量是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.为了使整个表达式为真,因此要采用if分支,任何变量为真就足够了。 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.为了使整个表达式为假,因此采用if分支,所有变量都必须为假。

You Don't Know JS has a good explanation too. You Don't Know JS也有很好的解释。

What exactly is taking place when you chain the values?当你链接价值时到底发生了什么?

In the first expression (&&) all the values must be truthy in order for the conditional test to pass.在第一个表达式 (&&) 中,所有值都必须为真值才能通过条件测试。 Assuming all values to be truthy the interpreter would evaluate all the values and the conditional test would pass.假设所有值都是真值,解释器将评估所有值并且条件测试将通过。 Otherwise it would evaluate up to the first falsey value and the conditional test would fail.否则它会评估到第一个 falsey 值并且条件测试将失败。

In the second expression (||) only one value must be truthy in order for the conditional test to pass.在第二个表达式 (||) 中,只有一个值必须是真值才能通过条件测试。 Assuming at least one value to be truthy the interpreter would evaluate up to the first truthy value and the conditional test would pass.假设至少一个值是真值,解释器将评估到第一个真值并且条件测试将通过。 Otherwise it would evaluate all the values and the conditional test would fail.否则,它将评估所有值,条件测试将失败。

According to MDN web docs : 根据MDN网站文档:
1-Logical AND (&&) - expr1 && expr2 => If expr1 can be converted to true, returns expr2; 1-逻辑与(&&)- expr1 && expr2 =>如果expr1可以转换为true,则返回expr2; else, returns expr1. 否则,返回expr1。
means that if expr1 is true it will return exper2 else it will return expr1. 表示如果expr1为true,则将返回exper2,否则将返回expr1。
2-Logical OR (||) - expr1 || 2逻辑或(||) -expr1 || expr2 => If expr1 can be converted to true, returns expr1; expr2 =>如果expr1可以转换为true,则返回expr1; else, returns expr2. 否则,返回expr2。
means that if expr1 is true output will be expr1 else output will be expr2. 表示如果expr1为true,则输出将为expr1,否则输出将为expr2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM