JavaScript Logical Operators
Logical operators in JavaScript are used for making decisions based on conditions and manipulating boolean values. They are commonly used to compare variables or values and set termination conditions for loops.
In JavaScript, there are basically three types of logical operators.
| OPERATOR NAME | OPERATOR SYMBOL | OPERATION |
|---|---|---|
| NOT | ! | Converts operator to boolean and returns flipped value |
| AND | && | Evaluates operands and return true only if all are true |
| OR | || | Returns true even if one of the multiple operands is true |
1. !(NOT) Operator
!(NOT) Operator reverses the boolean result of the operand (or condition). It first converts the operand to a boolean type and then returns its flipped value.
Example: In this example, we will perform the NOT operation on number multiple times.
// !(NOT) operator
let i = 0;
console.log((!i));
console.log(!!i);
Output:
true
falseExplanation: Since zero is treated as a falsy value therefore NOT operation on zero will return true and when this operation is performed again we get true as output.
2. &&(AND) Operator
The && operator accepts multiple arguments and evaluates the operator from left to right. It returns true only if all the operands that are evaluated are true
Example: In this example, we will perform and operation on different numbers and evaluate their output as a boolean
// &&(AND) operator
let i = 0, j=2, k=3, l=8;
console.log(Boolean(i&&j&&k));
console.log(Boolean(j&&k&&l));
Output:
false
trueExplanation: In JavaScript, the value of 0 when converted to zero is considered false so when performing “and” operation on 0 a falsy value is returned and we get false output otherwise true.
3. ||(OR) Operator
The OR operator is somewhat opposite of the ‘AND’ operator. It also evaluates the operator from left to right and returns true even if one operand is evaluated as true
Example: In this example, we will perform OR operation on different data types and check their boolean output.
// ||(OR) Operator
let i = 1;
let j = null;
let k = undefined;
let l = 0;
console.log(Boolean(j||k));
console.log(Boolean(i||l));
Output:
false
trueExplanation: null, undefined, and 0 are recognized as falsy values. So OR operation on null and undefined will give false whereas numbers except 0 are treated as true.
Usage Examples:
- NOT Operator: Reverses the boolean result of an operand.
- AND Operator: Returns true only if all evaluated operands are true.
- OR Operator: Returns true if at least one evaluated operand is true.
Supported Browsers: The browsers supported by all JavaScript Logical operators are listed below:
- Google Chrome
- Firefox
- Opera
- Safari
- Microsoft Edge
- Internet Explorer
We have a complete list of Javascript Operators, to check those please go through this Javascript Operators Complete reference article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
JavaScript Logical Operators – FAQs
What is short-circuit evaluation?
Short-circuit evaluation means that logical operators stop evaluating expressions as soon as the outcome is determined. For &&, if the first operand is false, the overall result is false, so it doesn’t evaluate the second operand. For ||, if the first operand is true, the overall result is true, so it doesn’t evaluate the second operand.
Can logical operators be used with non-boolean values?
Yes, logical operators can be used with non-boolean values. They return one of the original values instead of a boolean. For &&, if the first operand is falsy, it returns the first operand; otherwise, it returns the second operand. For ||, if the first operand is truthy, it returns the first operand; otherwise, it returns the second operand.
What are truthy and falsy values in JavaScript?
In JavaScript, a value is truthy if it is considered true in a boolean context, and falsy if it is considered false. Falsy values include false, 0, “” (empty string), null, undefined, and NaN. All other values are truthy.

