30. Logical Operators
Logical operators allow us to combine multiple boolean conditions.
1. Logical AND (&&)
Returns true only if both operands are true.
| A | B | A && B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
javascript let age = 20; let hasLicense = true;
// Must be 18 AND have a license let canDrive = (age >= 18) && hasLicense; console.log(canDrive); // true
2. Logical OR (||)
Returns true if at least one of the operands is true.
javascript let isWeekend = false; let isHoliday = true;
// We can relax if it is the weekend OR a holiday let canRelax = isWeekend || isHoliday; console.log(canRelax); // true
3. Logical NOT (!)
Reverses the boolean result (flips true to false and false to true).
javascript let isActive = true; console.log(!isActive); // false
Short-Circuiting (Advanced Tip)
&& and || evaluate from left to right. If the result can be determined early, the rest of the expression is skipped. For example, in A || B, if A is true, B is never checked.