ECMAScript Boolean operators
- Previous Page Bitwise Operators
- Next Page Multiplicative Operators
Boolean operators are very important as they enable the normal operation of programming languages.
There are three types of Boolean operators: NOT, AND, and OR.
ToBoolean operation
Before learning various logical operators, let's first understand the ToBoolean operation described in the ECMAScript-262 v5 specification.
The abstract operation ToBoolean converts its parameters according to the rules in the following table:
Parameter type | Result |
---|---|
Undefined | false |
Null | false |
Boolean | The result is equal to the input parameter (without conversion) |
Number | If the parameter is +0, -0, or NaN, the result is false; otherwise, it is true. |
String | If the parameter is an empty string, the result is false; otherwise, it is true. |
Object | true |
Logical NOT operator
In ECMAScript, the logical NOT operator is the same as the logical NOT operator in C and Java, both represented by the exclamation mark (!).
Unlike the logical OR and logical AND operators,The logical NOT operator always returns a Boolean value.
The behavior of the logical NOT operator is as follows:
- If the operand is an object, returns false
- If the operand is the number 0, returns true
- If the operand is any number other than 0, returns false
- If the operand is null, returns true
- If the operand is NaN, returns true
- If the operand is undefined, an error occurs
This operator is usually used to control loops:
var bFound = false; var i = 0; while (!bFound) { if (aValue[i] == vSearchValues) { bFound = true; } i++; } }
In this example, the Boolean variable (bFound) is used to record whether the retrieval is successful. When the data item in the problem is found, bFound will be set to true, and !bFound will be equal to false, meaning the execution will exit the while loop.
When determining the Boolean value of an ECMAScript variable, you can also use the logical NOT operator. To do this, you need to use two NOT operators in a single line of code. The first NOT operator returns a Boolean value regardless of the operand type. The second NOT inverts the Boolean value, thus giving the actual Boolean value of the variable.
var bFalse = false; var sRed = "red"; var iZero = 0; var iThreeFourFive = 345; var oObject = new Object; document.write("The logical value of bFalse is " + (!!bFalse)); document.write("The logical value of sRed is " + (!!sRed)); document.write("The logical value of iZero is " + (!!iZero)); document.write("The logical value of iThreeFourFive is " + (!!iThreeFourFive)); document.write("The logical value of oObject is " + (!!oObject));
Result:
The logical value of bFalse is false The logical value of sRed is true The logical value of iZero is false The logical value of iThreeFourFive is true The logical value of oObject is true
Logical AND operator
In ECMAScript, the logical AND operator is represented by double ampersands (&&):
For example:
var bTrue = true; var bFalse = false; var bResult = bTrue && bFalse;
The following truth table describes the behavior of the logical AND operator:
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
The operands of logical AND operations can be of any type, not just Boolean values.
If an operand is not an original Boolean type value, the logical AND operation does not necessarily return a Boolean value:
- If one operand is an object and the other is a Boolean value, return the object.
- If both operands are objects, return the second object.
- If an operand is null, return null.
- If an operand is NaN, return NaN.
- If an operand is undefined, an error occurs.
Similar to logical AND operations in Java, logical AND operations in ECMAScript are also simple operations, that is, if the first operand determines the result, the second operand will not be calculated. For logical AND operations, if the first operand is false, then no matter what the value of the second operand is, the result cannot be true.
Consider the following example:
var bTrue = true; var bResult = (bTrue && bUnknown); // An error occurs alert(bResult); // This line will not be executed
This piece of code will cause an error during logical AND operation because the variable bUnknown is undefined. The value of bTrue is true because the logical AND operation will continue to calculate the variable bUnknown. This will cause an error because the value of bUnknown is undefined and cannot be used in logical AND operation.
If you modify this example and set the first number to false, an error will not occur:
var bFalse = false; var bResult = (bFalse && bUnknown); alert(bResult); // Outputs "false"
In this piece of 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 will not be calculated because the value of the first operand is false.
Tip:When using the logical AND operator, remember its simple calculation feature.
Logical OR Operator
The logical OR operator in ECMAScript is the same as in Java, represented by double vertical bars (||):
var bTrue = true; var bFalse = false; var bResult = bTrue || bFalse;
The following truth table describes the behavior of the logical OR operator:
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Similar to the logical AND operator, if an operand is not a Boolean value, the logical OR operator does not necessarily return a Boolean value:
- If an operand is an object and all operands to the left are false, then return the object.
- If both operands are objects, return the first object.
- If the last operand is null and all other operands are false, then return null.
- If the last operand is NaN and all other operands are false, then return NaN.
- If an operand is undefined, an error occurs.
Like the logical AND operator, logical OR is also a simple operation. For the logical OR operator, if the first operand is true, the second operand will not be calculated.
For example:
var bTrue = true; var bResult = (bTrue || bUnknown); alert(bResult); // Output "true"
Like the previous example, the variable bUnknown is undefined. However, since the value of bTrue is true, bUnknown will not be calculated, so the output is "true".
If bTrue is changed to false, an error will occur:
var bFalse = false; var bResult = (bFalse || bUnknown); // An error occurs alert(bResult); // This line will not be executed
- Previous Page Bitwise Operators
- Next Page Multiplicative Operators