Look at two questions first:
The first question why a && b returns is True,b && a returns 6
1 var user = 6; 2 var true ; 3 4 console.log (user && both); // true 5 Console.log (both && user); // 6
The second question both to self-subtract the last print is still 3
1 var false ; 2 var both = 3; 3 4 console.log (user && both--); // false 5 Console.log (both); // 3
Start with three levels of understanding (&& for example):
The first level of understanding: when the operand is a Boolean value, only if both are true, the result is true, otherwise false.
The second layer understands that:&& can operate on true and false values, and if two are true, returns a true value, otherwise returns a dummy. However, the truth value is not limited to true, false values are not limited to false, and the following values are converted to false (all other values are true)
Undefined
Null
0/-0
NaN
" "
In JS, the && operation, the result is not always true and false, but the current value. The current value is likely to be numeric, string, and so on.
If the first expression is true, then the value of the second expression is the result, and the result is not the converted value, but itself.
If the first expression is false, then the value of the first expression is the result, and the result is not the converted value, but itself.
//The first question above
1 varuser = 6;2 varboth =true;3 varMath = 0;4Console.log (user && both);//true5Console.log (both && user);//66Console.log (both && math);//07Console.log (math && both);//0
The third layer of understanding: short-circuit operations, operations, first calculate the value of the left operand, if the result is a false value, then the entire expression result is a false value, that is, the value of the left operand at the same time stop the right operand evaluation. If the result of the left operand is true, the entire result depends on the value of the right operand.
Description of the short-circuit operation:
In && | | Operation, if the value of the first expression is already able to determine the result of the entire operation, then the second expression will not execute
For the && operation, the value of the first expression is false, which determines that the result of the operation is false, so the first expression does not execute, so the value of y--does not execute Y is still 3.
The second question above
1varfalse; 2 var y = 3; 3 console.log (x && y--); // false 4 console.log (y); // 3
for | | For
If the value of the first expression is true, the result is the value of the first expression, not after the conversion, but rather itself.
If the value of the first expression is a false value, the result is the value of the second expression, not after the conversion, but by itself.
1 var true ; 2 var both = ten; 3 4 console.log (user | | ++both); // true 5 Console.log (both); // Ten
&& | | Have exactly the same principle, except that the arithmetic rules are not quite the same.
1 varA = 10;2 varb =true;3 varc = 0;4 5Console.log (A | | b);//Ten6Console.log (b | | a);//true7 8Console.log (b | | c);//true9Console.log (c | | b);//true
javascript-in-depth understanding of && and | |