Comparison vs Logical Operators
You'll learn how comparison operators are evaluated before logical operators, which affects how JavaScript reads expressions like x > 5 && y < 10.
The Core Idea
When you write a complex condition combining comparisons and logical operators, JavaScript doesn't read them left-to-right equally. Comparison operators have higher precedence than logical operators, meaning JavaScript evaluates all the comparisons first, then applies the logical operations.
Think of it like a math problem: just as 3 + 4 * 2 evaluates the multiplication before addition (giving you 11, not 14), the expression x > 5 && y < 10 evaluates both > comparisons first, then applies the &&.
How It Works in Practice
let x = 7;
let y = 8;
// This expression:
x > 5 && y < 10
// Is evaluated like this:
// Step 1: x > 5 → true
// Step 2: y < 10 → true
// Step 3: true && true → true
The comparisons (> and <) happen automatically before the logical AND (&&) gets involved. You don't need parentheses because precedence handles it for you.
// These are equivalent:
x > 5 && y < 10
(x > 5) && (y < 10) // Parentheses are optional but clear
// Same with OR:
x < 3 || y > 15
(x < 3) || (y > 15)
Why This Matters
Without this precedence rule, x > 5 && y < 10 would be confusing or require parentheses everywhere. JavaScript automatically groups the comparisons first, making your conditions read naturally.
Key Takeaway: Comparison operators (>, <, >=, <=, ==, ===) bind tighter than logical operators (&&, ||), so comparisons always evaluate first in combined expressions.