What is a Boolean?
A Boolean is a data type with only two possible values: true or false. Booleans are the foundation of conditional logic — every if statement, loop condition, and logical expression evaluates to a boolean.
Declaring Booleans
bool isLoggedIn = true;
bool hasErrors = false;
bool isAdult = true;
var isDarkMode = false; // type inferred as bool
Boolean Operators
| Operator | Name | Example | Result |
&& | AND | true && false | false |
|| | OR | true || false | true |
! | NOT | !true | false |
bool a = true;
bool b = false;
print(a && b); // false (both must be true)
print(a || b); // true (at least one must be true)
print(!a); // false (NOT true = false)
print(!b); // true (NOT false = true)
Comparison Results
Comparison operators always return a boolean:
print(5 > 3); // true
print(5 < 3); // false
print(5 == 5); // true
print(5 != 3); // true
print(5 >= 5); // true
print(5 <= 4); // false
Example
void main() {
int age = 20;
bool hasID = true;
double balance = 50.0;
bool canEnter = age >= 18 && hasID;
bool canAfford = balance >= 30.0;
print('Can enter: $canEnter'); // true
print('Can afford ticket: $canAfford'); // true
print('All good: $${canEnter && canAfford}'); // true
// Negation
bool isMinor = !(age >= 18);
print('Is minor: $isMinor'); // false
}
Output:
Can enter: true
Can afford ticket: true
All good: true
Is minor: false
⚠️ Common Mistakes
- Using
= (assignment) instead of == (comparison): if (x = 5) is invalid in Dart.
- Treating non-zero integers as truthy — Dart does NOT allow
if (1). You must use an actual bool.
- Confusing
&& with & — use && for logical AND in conditions.
📝 Note: Unlike JavaScript or C, Dart does not have "truthy" or "falsy" values. Only actual bool values (true/false) can be used in conditions.
🧠 Quiz
1. What is the result of true && false?
- A) true
- B) false ✅
- C) null
- D) Error
2. Can you use if (1) in Dart?
- A) Yes, 1 is truthy
- B) No, Dart requires a bool ✅
- C) Only in debug mode
- D) Yes, it means true
Summary
Booleans in Dart hold only true or false. Use && (AND), || (OR), and ! (NOT) for boolean logic. Comparison operators return booleans. Dart requires actual boolean values in conditions — non-zero integers are NOT truthy.