Home > Article > Web Front-end > In-depth understanding of && and || examples in js
Let’s start with two questions:
The first question is why a && b returns true, b && a returns 6
1 var user = 6;2 var both = true;3 4 console.log( user && both); //true5 console.log( both && user); //6
The second question is both Self-decrementing, the final printed value is still 3
1 var user = false;2 var both = 3;3 4 console.log( user && both--); // false5 console.log(both); // 3
Let’s understand it from three levels (taking && as an example):
The first level of understanding: when the operands are all Boolean values, Only when both are true, the result is true, otherwise it is false.
Second level understanding: && can operate on true values and false values. If both are true values, a true value is returned, otherwise a false value is returned. However, true values are not limited to true, and false values are not limited to false. The following values will be converted to false (all other values are true)
undefined
null
0/-0
NaN
" "
In js, when performing && operation, the result is not always true or false, but the current value . The current value may be a numerical value, a string, etc.
If the first expression is true, then the value of the second expression is used as its result. This result is not the converted value, but itself.
If the first expression is false, then the value of the first expression is used as the result. This result is not the converted value, but itself.
user = 6 both = math = 0 console.log( user && both); console.log( both && user); console.log( both && math); console.log( math && both);
The third level of understanding: short-circuit operation, the operation process, first calculates the value of the left operand, if the calculation result is a false value, then the result of the entire expression is a false value, that is, the value of the left operand Also stops evaluation of the right operand. If the result of the left operand is true, the entire result depends on the value of the right operand.
Explanation on short-circuit operations:
In && and || operations, if the value of the first expression can already determine the result of the entire operation, then the second expression will Will not be executed
For the && operation, the value of the first expression is false, which determines that the entire operation result is false, so the first expression will not be executed, so y-- is not executed. The value of y is still 3
x = y = 3 console.log(x && y--); console.log(y);
For ||,
If the value of the first expression is true, the result is the value of the first expression, not after conversion , but itself.
If the value of the first expression is false, the result is the value of the second expression, not after conversion, but itself.
1 var user = true;2 var both = 10;3 4 console.log(user || ++both); //true5 console.log(both); //10
&& has exactly the same principle as ||, but the operation rules are different.
1 var a = 10;2 var b = true;3 var c = 0;4 5 console.log(a || b); //106 console.log(b || a); //true7 8 console.log(b || c); //true9 console.log(c || b); //true
The above is the detailed content of In-depth understanding of && and || examples in js. For more information, please follow other related articles on the PHP Chinese website!