Boolean Logic
Deepen your understanding of true/false values, logical operators, truthiness, and short-circuit evaluation.
What you'll learn
- The three logical operators: AND, OR, NOT
- What truthy and falsy values are in JavaScript
- Short-circuit evaluation and its practical uses
- How to combine and negate conditions effectively
- De Morgan's laws for simplifying complex conditions
Concept
Boolean Logic
Boolean logic is the foundation of all decision-making in programming. Every if statement, every loop condition, and every filter operation ultimately evaluates to a single boolean value: true or false. Understanding how logical operators combine and transform these values is critical for writing correct conditions.
The Three Logical Operators
- AND (
&&) — returnstrueonly if *both* operands are true. Think of it as "all conditions must pass." - OR (
||) — returnstrueif *either* operand is true. Think of it as "at least one condition must pass." - NOT (
!) — inverts a boolean:!trueisfalse,!falseistrue.
const age = 25;
const hasLicense = true;
const canDrive = age >= 16 && hasLicense; // true (both conditions met)
const isWeekend = false;
const isHoliday = true;
const dayOff = isWeekend || isHoliday; // true (at least one is true)
const isBlocked = !canDrive; // false (inverted)
Truthy and Falsy
JavaScript does not require strict boolean values in conditions. Every value is classified as either truthy or falsy. The falsy values are: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy — including non-empty strings, non-zero numbers, and all objects.
if ("hello") { /* runs — non-empty string is truthy */ }
if (0) { /* skipped — 0 is falsy */ }
if ([]) { /* runs — empty array is truthy! */ }
Short-Circuit Evaluation
The && and || operators short-circuit: they stop evaluating as soon as the result is determined. For &&, if the left side is falsy, the right side is never evaluated. For ||, if the left side is truthy, the right side is never evaluated.
This is not just a performance detail — it is a powerful idiom for providing default values and guarding against undefined:
const config = userSettings || defaults; // use defaults if userSettings is falsy
const name = user && user.name; // safe access: won't error if user is null
Combining Conditions
Complex conditions can be built by chaining operators. Use parentheses to make the grouping explicit — relying on default precedence is a common source of bugs:
if ((isAdmin || isEditor) && !isSuspended) {
// allow access
}
De Morgan's Laws
Two transformation rules help simplify and invert conditions: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b. These laws let you rewrite negated compound conditions into simpler forms, which often read more naturally.
Example
// Form validation using boolean logic
function isFormValid(email, password, age) {
const hasEmail = email.includes("@") && email.length > 5;
const hasPassword = password.length >= 8;
const isAdult = age >= 18;
// All three must be true (AND logic)
return hasEmail && hasPassword && isAdult;
}
// Short-circuit for default values
function getDisplayName(user) {
// If user is null OR user.name is empty, fall back to "Guest"
return user?.name || "Guest";
}
// Using NOT to check for absence
const errors = [];
const isValid = !errors.length; // true when array is empty (0 is falsy)
console.log(isFormValid("[email protected]", "secure123", 25)); // true
console.log(isFormValid("bad", "short", 15)); // false
console.log(getDisplayName(null)); // "Guest"
console.log(isValid); // true
The validator combines three conditions with AND. The display name function uses short-circuit OR for a default. The isValid check uses NOT on the array length — an empty array has length 0 (falsy), so !0 is true.
Try it
- Password Strength Analyser
Password rules are boolean logic in action: length >= 8 AND hasUpper AND hasDigit AND hasSymbol.
- Regex Tester
Regular expressions ultimately produce boolean results (match or no match). Test patterns and see the logic.
Common mistakes
The mistake
Using ! to negate a compound condition without parentheses.
The fix
!(a && b) negates the whole expression. !a && b only negates a. Always use parentheses around the expression you want to invert.
The mistake
Assuming empty arrays and objects are falsy.
The fix
In JavaScript, [] and {} are truthy. Only false, 0, '', null, undefined, and NaN are falsy. Check array.length === 0 or Object.keys(obj).length === 0 instead.
The mistake
Relying on operator precedence instead of using parentheses.
The fix
Always use parentheses in compound conditions. !(a || b) && c is clearer than relying on the default precedence of ! over && over ||.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Reference for all logical operators, truthy/falsy values, and short-circuit behavior.
Practice Boolean Logic
9 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.