Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

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). If the test is false, the || expression results in the value of the second operand (b).

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
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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. 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.

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.

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.

You Don't Know JS has a good explanation too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...