Boolean Algebra
Combine true/false values with AND, OR, and NOT.
Quick Summary
Boolean algebra powers logical checks in programs. JavaScript uses short-circuiting for && and ||, so the runtime may stop evaluating once the result is known. This enables patterns like default values and guard clauses.
Boolean Logic Fundamentals
Boolean algebra deals with true/false values and three main operations:
AND (&&)
Both must be true:
true && true = true
true && false = false
false && true = false
false && false = false
OR (||)
At least one must be true:
true || true = true
true || false = true
false || true = true
false || false = false
NOT (!)
Inverts the value:
!true = false
!false = true
Truth Tables
| A | B | A && B | A || B | !A | |---|---|--------|---------|-----| | T | T | T | T | F | | T | F | F | T | F | | F | T | F | T | T | | F | F | F | F | T |
Short-Circuit Evaluation
JavaScript stops evaluating as soon as result is known:
// AND: stops at first false
false && expensiveFunction(); // Never calls function
// OR: stops at first true
true || expensiveFunction(); // Never calls function
Practical Patterns
Default values:
const name = userInput || "Guest";
// If userInput is falsy, use "Guest"
Guard clause:
user && user.name; // Only access if user exists
// Modern: user?.name (optional chaining)
Nullish coalescing (??):
const count = input ?? 0;
// Only use 0 if input is null/undefined
// (unlike ||, doesn't treat 0 or "" as falsy)
Try It Yourself
Here's a practical example you can try. Copy this code and run it in your browser's console (press F12 to open developer tools) or in the Code Playground.
// Basic boolean operations
const a = true;
const b = false;
console.log(a && b); // false (AND)
console.log(a || b); // true (OR)
console.log(!a); // false (NOT)
console.log(!b); // true (NOT)
// Combining conditions
const age = 25;
const hasLicense = true;
const hasInsurance = true;
const canDrive = age >= 16 && hasLicense;
console.log("Can drive:", canDrive); // true
const canRentCar = age >= 21 && hasLicense && hasInsurance;
console.log("Can rent car:", canRentCar); // true
// Short-circuit evaluation
const user = null;
const username = user && user.name; // null (stops at user)
console.log(username);
// Default values with ||
const inputName = "";
const displayName = inputName || "Anonymous";
console.log(displayName); // "Anonymous"
// Nullish coalescing (??) - only null/undefined trigger default
const count = 0;
const displayCount = count ?? 10; // 0 (not replaced)
const displayCount2 = count || 10; // 10 (0 is falsy)
// De Morgan's Laws
// !(A && B) === !A || !B
// !(A || B) === !A && !B
const x = true, y = false;
console.log(!(x && y) === (!x || !y)); // true
console.log(!(x || y) === (!x && !y)); // trueKey Takeaways
- ✓Combine true/false values with AND, OR, and NOT.
- ✓Practice with real code examples to solidify your understanding
- ✓This concept builds the foundation for more advanced topics
Related Learning Resources
Continue your programming journey with these related tutorials: