What is Operator Precedence?
You'll learn how JavaScript decides which operations to perform first when you mix multiple operators in a single expression.
The Core Idea
When you write an expression like 5 + 3 * 2, should JavaScript add first or multiply first? The answer is operator precedence — a built-in set of rules that determines the order of operations, just like the math rules you learned in school (remember "Please Excuse My Dear Aunt Sally"?).
JavaScript follows similar rules: certain operators have higher precedence and execute before others, even if they appear later in the expression.
Understanding the Hierarchy
Think of operator precedence like a hierarchy of importance. When JavaScript encounters multiple operators, it checks their precedence levels and performs higher-precedence operations first:
let result = 5 + 3 * 2; // result is 11, not 16
Why 11? Because multiplication (*) has higher precedence than addition (+). JavaScript multiplies 3 * 2 first (getting 6), then adds 5.
The Parentheses Override
Just like in math, parentheses () have the highest precedence of all. They force JavaScript to evaluate what's inside first:
let result = (5 + 3) * 2; // result is 16
Now addition happens first because it's wrapped in parentheses.
Why This Matters
Without understanding precedence, you might write expressions that produce unexpected results. Knowing the order helps you predict outcomes and use parentheses strategically for clarity.
Key Takeaway: JavaScript uses operator precedence rules to determine which operations execute first in complex expressions, with multiplication and division taking priority over addition and subtraction, and parentheses overriding everything.