Logical AND
The logical AND operator is &&.
var result = true && false;
Logical AND behaves in the following truth table:
OPERAND 1 | OPERAND 2 | RESULT |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Logical AND can work on any type of operand, not just Boolean values.
When logical AND working with non-Boolean values:
OPERAND 1 | OPERAND 2 | RESULT |
---|---|---|
object | any value | return the second operand |
and value | object | return the second object if the first operand evaluates to true. |
object | object | return the second operand |
null | any value | null |
any value | null | null |
NaN | any value | NaN |
any value | NaN | NaN |
undefined | any value | undefined |
any value | undefined | undefined |
Short-circuited operation
The logical AND operator is a short-circuited operation. The short-circuited operation means that if the first operand determines the result, the second operand is not evaluated. For logical AND, if the first operand is false, the second operand is not evaluated since the result is false.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var found = false;
var i = 1;
var result = (found && i++);
document.writeln(result); //false
document.writeln(i); //1
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator