JavaScript has four logical operators: || (OR), && (AND), ! (NOT), ?? (Nullish Coalescing). Logical operators are used to determining the logic between variables or values.
| Operator | Description | Example |
| && | and | (x < 10 && y > 1) is true |
| || | or | (x == 5 || y == 5) is false |
| ! | or | !(x == y) is true |
JavaScript logical operators
A simple example code compares variables and does something based on the result of that comparison.
The Logical AND operator (&&)
The double ampersand (&&) to represent the logical AND operator. The following truth table illustrates the result of the && operator when it is applied to two Boolean values:
| a | b | a && b |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Exmaple
<!DOCTYPE html>
<html>
<body>
<script>
console.log( true && true );
console.log( false && true );
console.log( true && false );
console.log( false && false );
</script>
</body>
</html>Output:

The Logical OR operator (||)
The doubles pipe || represents the logical OR operator. You can apply the || operator to two values of any type.
The following truth table illustrates the result of the || operator based on the value of the operands:
| a | b | a || b |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Example
console.log( true || true ); // true
console.log( false || true ); // true
console.log( true || false ); // true
console.log( false || false ); // falseThe Logical NOT operator (!)
The exclamation point! represents the logical NOT operator. The ! operator can be applied to a single value of any type, not just a Boolean value.
The operator accepts a single argument and does the following:
- Converts the operand to boolean type:
true/false. - Returns the inverse value.
alert( !true ); // false
alert( !0 ); // trueThe logical ! the operator works based on the following rules:
- If
aisundefined, the result istrue. - If
aisnull, the result istrue. - If
ais a number other than0, the result isfalse. - If
aisNaN, the result istrue. - If
aisnull, the result istrue. - If
ais an object, the result isfalse. - If
ais an empty string, the result istrue. In the caseais a non-empty string, the result isfalse
console.log(!undefined); // true
console.log(!null); // true
console.log(!20); //false
console.log(!0); //true
console.log(!NaN); //true
console.log(!{}); // false
console.log(!''); //true
console.log(!'OK'); //false
console.log(!false); //true
console.log(!true); //falseDo comment if you have any doubts or suggestions on this Js operator topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version