JavaScript Logical Operators are used to perform logical operations and return boolean results. They are commonly used in conditional statements and loops to make decisions based on multiple conditions.
List of Logical Operators:
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | true && false |
false |
` | ` | Logical OR | |
! |
Logical NOT | !true |
false |
?? |
Nullish Coalescing | null ?? "default" |
"default" |
?. |
Optional Chaining | obj?.prop |
undefined |
1. Logical AND (&&
)
The Logical AND operator returns true
if both operands are true
. If one or both operands are false
, it returns false
.
Syntax:
expr1 && expr2
Example:
let a = true; let b = false; console.log(a && b); // false console.log(a && true); // true
2. Logical OR (||
)
The Logical OR operator returns true
if at least one of the operands is true
. If both operands are false
, it returns false
.
Syntax:
expr1 || expr2
Example:
let a = true; let b = false; console.log(a || b); // true console.log(b || false); // false
3. Logical NOT (!
)
The Logical NOT operator reverses the truthiness of its operand. If the operand is true
, it returns false
, and vice versa.
Syntax:
!expr
Example:
let a = true; console.log(!a); // false console.log(!false); // true
4. Nullish Coalescing (??
)
The Nullish Coalescing operator returns the right-hand operand if the left-hand operand is null
or undefined
. Otherwise, it returns the left-hand operand.
Syntax:
expr1 ?? expr2
Example:
let a = null; let b = "default"; console.log(a ?? b); // "default" console.log("value" ?? b); // "value"
5. Optional Chaining (?.
)
The Optional Chaining operator allows safe access to deeply nested properties of an object, returning undefined
if the property doesn’t exist, instead of throwing an error.
Syntax:
obj?.prop obj?.[expr]
Example:
let obj = { name: "John" }; console.log(obj?.name); // "John" console.log(obj?.age); // undefined
Combining Logical Operators:
Logical operators can be combined to evaluate complex conditions.
Example:
let age = 25; let hasLicense = true; if (age > 18 && hasLicense) { console.log("Eligible to drive"); } else { console.log("Not eligible to drive"); } // Output: Eligible to drive
Operator Precedence:
Logical operators have different levels of precedence:
- Logical NOT (
!
): Highest precedence. - Logical AND (
&&
): Higher than OR but lower than NOT. - Logical OR (
||
): Lowest precedence among logical operators.
Parentheses can be used to group expressions and control the order of evaluation.
Example:
let result = true || false && !false; // Without parentheses, the order of operations is: // 1. `!false` evaluates to `true` // 2. `false && true` evaluates to `false` // 3. `true || false` evaluates to `true` console.log(result); // true
Summary:
&&
(AND): Both conditions must betrue
for the result to betrue
.||
(OR): At least one condition must betrue
for the result to betrue
.!
(NOT): Inverts the boolean value.??
(Nullish Coalescing): Returns the right operand if the left isnull
orundefined
.?.
(Optional Chaining): Safely accesses properties that might not exist.
Logical operators are essential for making decisions in JavaScript, allowing for the evaluation of multiple conditions and the implementation of control flow in your code.