Introduction
Logical operators combine multiple boolean expressions. Dart provides three logical operators: AND (&&), OR (||), and NOT (!). They are used to build complex conditions.
Why It Matters
Real-world conditions are rarely simple. A user might need to be logged in AND have an admin role, or a form field might be valid if it is not empty OR has a default value. Logical operators let you express these multi-part conditions concisely.
Logical Operators
| Operator | Name | Description |
&& | AND | Returns true only if BOTH sides are true |
|| | OR | Returns true if AT LEAST ONE side is true |
! | NOT | Reverses the boolean value |
Truth Table
| A | B | A && B | A || B | !A |
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
Code Example
void main() {
bool isLoggedIn = true;
bool isAdmin = false;
bool hasSubscription = true;
// AND: both must be true
print(isLoggedIn && isAdmin); // false
print(isLoggedIn && hasSubscription); // true
// OR: at least one must be true
print(isAdmin || hasSubscription); // true
print(isAdmin || false); // false
// NOT: reverses the value
print(!isAdmin); // true
print(!isLoggedIn); // false
// Complex condition
int age = 25;
bool hasId = true;
if (age >= 18 && hasId) {
print('Entry allowed');
}
// Short-circuit evaluation
int x = 0;
if (x != 0 && (100 ~/ x) > 5) {
print('Safe division');
} else {
print('Skipped to avoid division by zero');
}
}
Output:
false
true
true
false
true
false
Entry allowed
Skipped to avoid division by zero
Explanation
&& (AND) returns true only when all operands are true. It short-circuits: if the left side is false, the right side is never evaluated.
|| (OR) returns true when at least one operand is true. It short-circuits: if the left side is true, the right side is never evaluated.
! (NOT) flips true to false and false to true.
Notes
- Short-circuit evaluation is a performance optimization and a safety feature — use it to avoid null dereferences or division by zero.
- Use parentheses to clarify operator precedence in complex expressions:
(a && b) || c vs a && (b || c).
- Dart does NOT support bitwise logical operators (
&, |) on booleans the same way — always use && and || for boolean logic.
Common Mistakes
- Using single
& or | instead of && or || for boolean logic — these are bitwise operators.
- Forgetting short-circuit behavior and placing expensive or side-effect operations on the right side of
&& when the left may be false.
- Double-negating unnecessarily:
!!isAdmin is just isAdmin.
💪 Exercise
- Write a login check: a user can access the dashboard if they are logged in AND (are an admin OR have a subscription).
- Write a condition that prints "Valid input" if a string is not empty and its length is less than 50.
- Use the NOT operator to toggle a boolean
isDarkMode variable.
🧠 Quiz
1. What does true && false || true evaluate to?
- A) false
- B) true ✅
- C) null
- D) error
2. Which feature prevents the right side of && from being evaluated when the left is false?
- A) Lazy loading
- B) Short-circuit evaluation ✅
- C) Null safety
- D) Type inference
Summary
Logical operators (&&, ||, !) combine boolean expressions to form complex conditions. AND requires all conditions to be true; OR requires at least one; NOT reverses a value. Dart uses short-circuit evaluation, meaning the second operand is only evaluated when necessary. Use parentheses to control precedence in complex expressions.