This article mainly introduces the logic And operator in JavaScript. In JavaScript, the logic AND operator is expressed in JavaScript with a pair And sign (& amp, the logical AND operator is expressed by the double AND sign (&).
var bTrue = true;var bFalse = false;var bResult = bTrue && bFalse;
The following truth table describes the behavior of the logical AND operator:
It should be noted that the number of logical AND operations can be of any type, not only a Boolean value. If a certain number is not the original Boolean value, the logical AND operation does not necessarily return a Boolean value.
The operation behavior of the logical AND operator is as follows:
- If one operation is an object and the other is a Boolean value, this object is returned.
- If both operations are objects, the second object is returned.
- If the number of operations is null, null is returned.
- If the number of operations is NaN, NaN is returned.
- An error occurs if the number of operations is undefined.
- If both operations are of the boolean type, the return value is boolean.
Similar to the logic AND operation in Java, the logic AND operation in JavaScript is also a simple operation. That is, if the first operation determines the result, the second operation is no longer calculated. For logical AND operations, if the first operation is false, no matter what the value of the second operation is, The result cannot be equal to true.
Consider the following example:
Var bTrue = true; var bResult = (bTrue & bUnknown); // Error alert (bResult); // This row will not be executed
Code running result:
This code will cause an error when performing logical AND operations, because the variable bUnknown is undefined. The value of the variable bTrue is true, because the logic AND operation will continue to calculate the variable bUnknown. This will cause an error because the bUnknown value is undefined AND cannot be used for logical AND operations.
If you modify this example and set the first number to false, no error will occur:
Var bTrue = false; var bResult = (bTrue & bUnknown); // No error alert ("bTrue & bUnknown Result:" + (bResult )); // output "false"
In this Code, the script outputs the value returned by the logical AND operation, that is, the string "false ". Even if the value of the variable bUnknown is undefined, It is not calculated because the value of the first operation is false.
Running result:
Verify the operation behavior of the JavaScript logic And operator
Test code: