Combining Multiple Logical Operators
You'll learn how to chain &&, ||, and ! together in a single expression and understand which operators evaluate first.
The Core Idea
Just like math has order of operations (multiplication before addition), JavaScript's logical operators also have precedence rules. When you combine &&, ||, and ! in one expression, JavaScript evaluates them in a specific order:
!(NOT) — highest precedence, evaluated first&&(AND) — evaluated second||(OR) — lowest precedence, evaluated last
Think of it like this: negations happen immediately, then "ands" group together, and finally "ors" connect everything.
How It Works in Practice
let result = !false && true || false;
// Step 1: !false → true
// Step 2: true && true → true
// Step 3: true || false → true
console.log(result); // true
You can use parentheses to override the default order, just like in math:
let a = true || false && false;
// && happens first: false && false → false
// Then ||: true || false → true
console.log(a); // true
let b = (true || false) && false;
// () forces ||: true || false → true
// Then &&: true && false → false
console.log(b); // false
Why This Matters
Complex conditions are common in real code:
let isLoggedIn = true;
let hasPermission = false;
let isAdmin = true;
let canAccess = isLoggedIn && (hasPermission || isAdmin);
// isLoggedIn && (false || true)
// isLoggedIn && true
// true
Key Takeaway: When combining logical operators, ! evaluates first, then &&, then ||. Use parentheses to make your intent clear and override default precedence.