Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- logical AND operator
- logical OR operator
- boolean operand
- precedence
- associativity
- binary operator
- boolean
- Another member of the logical operators is the logical NOT operator. The ECMAScript Language Specification includes this operator with the unary operators. And this is where we choose to cover it also.
- Synonyms:
- Boolean Operators: these operators evaluate there operation result to either true or false. Thus the term "Boolean Operators" is appropriate.

This page will show the basic rules for JavaScript logical operator group. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The logical operators are binary operators. They have a "left to right" associatively. The logical AND has a precedence of 5 and the logical OR has a precedence of 4.
- The logical operands are evaluated to a boolean value and the logical operation is applied by the operator.
- The AND operator evaluates the result of the operation to be true only when both operands are true.
- The OR operator evaluates the result of the operation to be true when either operand is true.
- The logical operators can be better visualized with the following truth table:

Logical Operator Truth Table Operand 1 operand 2 AND Result OR Result true true true true true false false true false true false true false false false false - Truncated Operations:
- Logical AND: In the table above, the cells with green background represent a condition where the AND operator will never evaluate. If the first operand evaluates to false, the result will be false and therefore the evaluation of the second operand is immaterial. And, thusly, it is not evaluated.
- Logical OR: In the table above, the cells with orange background represent a condition where the OR operator will never evaluate. If the first operand evaluates to true, the result will be true and therefore the evaluation of the second operand is immaterial.

- The value produced by the logical operators may not always be a boolean. However, the value produced will always be the value of one of the two operands. Here we list these situations.
- Logical AND Operator:
- If either operand is NaN, NaN is returned (except for condition 6 below).
- If either operand is null, null is returned (except for condition 6 below).
- If both operands is a object, the 2nd operand is returned.
- If one operand is an object and the other operand is a boolean, the object is returned. (except for condition 6 below)
- If either operand is undefined, an error occurs.
- With 1, 2 and 4 above, when the first operand evaluates to false, the second operand is not evaluated and false is returned.

- Logical OR Operator:
- If either operand is NaN, NaN is returned (except for condition 6 below).
- If both operands are null, null is returned.
- If both operands is a object, the 1st operand is returned.
- If one operand is an object and the other operand is a boolean, the object is returned. (except for condition 6 below)
- If either operand is undefined, an error occurs.
- With 1 and 4 above, when the first operand evaluates to true, the second operand is not evaluated and true is returned.

Expression | Giving x= | Comment |
---|---|---|

var x = true, y = true; alert(x && y); |
true | Logical AND Operation both operands evaluate to be true giving a true result |

var x = true, y = true; alert(x || y); |
true | Logical OR Operation either operand may evaluate to be true giving a true result |

var x = false, y = true; alert(x && y); |
false | Logical AND Operation both operands must evaluate to be true to give a true result |

var x = false, y = true; alert(x || y); |
true | Logical OR Operation one operand evaluates to be true giving a true result |

var x = false, y = false; alert(x && y); |
false | Logical AND Operation both operands evaluate to be false to give a false result |

var x = false, y = false; alert(x || y); |
false | Logical OR Operation neither operand evaluates to be true giving a false result |