JavaScript Operator Precedence Complete Guide with Examples
Operator precedence is the hidden rule that determines which part of a JavaScript expression runs first. Learn the order, see real examples, and stop guessing.
Operator precedence is the set of rules that determines the order JavaScript evaluates parts of an expression. It is why an expression like 5 + 3 * 2 does not just evaluate left to right. Without these rules, expressions with multiple operators would be ambiguous.
Think of it like the order of operations you learned in math class (PEMDAS). JavaScript has the same idea, but with many more operators.
Why Precedence Matters
Precedence decides which operator runs first when an expression mixes several of them. Run these two lines and notice the difference a single pair of parentheses makes:
console.log(5 + 3 * 2); // 11
console.log((5 + 3) * 2); // 16Multiplication has higher precedence than addition, so the first line multiplies before it adds, giving 11. Parentheses in the second line override that default order and force the addition to run first, giving 16.
If JavaScript just read left to right with no precedence rules, both lines would produce the same result. Precedence is what makes expressions behave predictably instead of ambiguously.
The Precedence Table
Here is the order from highest priority to lowest, covering the operators you will use most often:
| Precedence | Operator(s) | Description | Associativity |
|---|---|---|---|
| 1 (highest) | () | Grouping | n/a |
| 2 | ** | Exponentiation | right-to-left |
| 3 | * / % | Multiply, divide, remainder | left-to-right |
| 4 | + - | Add, subtract | left-to-right |
| 5 | > < >= <= | Comparison | left-to-right |
| 6 | === !== == != | Equality | left-to-right |
| 7 | && | Logical AND | left-to-right |
| 8 | ` | ` | |
| 9 (lowest) | = += -= | Assignment | right-to-left |
Associativity decides the direction when operators at the same level compete. Left-to-right means the leftmost operator runs first. Right-to-left means the rightmost runs first.
Precedence in Action
Arithmetic before comparison
Comparison operators have lower precedence than arithmetic, so math runs before the check:
console.log(10 + 5 > 12); // true: (10 + 5) > 12, then 15 > 12This reads naturally: calculate the sum first, then compare the result. If you need a refresher on the operators themselves, see the operators overview.
Comparison before logical
Comparison operators have higher precedence than logical operators, so each comparison runs before they are combined:
const age = 25;
const hasLicense = true;
console.log(age >= 18 && hasLicense); // true: (age >= 18) && hasLicenseJavaScript checks the age comparison first. Since it is true, it then evaluates the AND with hasLicense, which is also true. You do not need parentheses here, but they do not hurt readability.
AND before OR
AND has higher precedence than OR, the same way multiplication has higher precedence than addition:
console.log(true || false && false); // true
// What actually happens:
// false && false evaluates first -> false
// true || false evaluates next -> trueThis reads ambiguously at first glance. The expression above behaves like OR combined with a parenthesized AND on the right, not like an AND applied across the whole line. If you intended the AND to apply first across both sides, you need to add parentheses yourself.
Because AND-before-OR is easy to misread, always use parentheses when mixing AND and OR. This is also why logical short-circuiting behaves differently when you nest AND and OR without parentheses:
// Clear intent, no guessing needed
const result = (isWeekend || isHoliday) && storeIsOpen;Assignment Operators Come Last
Assignment has the lowest precedence of all. Everything on the right side evaluates completely before the result is assigned:
let total = 10 + 5 * 2; // 10 + 10 = 20, then total = 20JavaScript calculates the entire right-hand expression first, following normal precedence, then assigns the final result to total. The assignment always happens last.
Common Precedence Pitfalls
Mixing string concatenation with arithmetic
The plus operator does both addition and string concatenation. When both appear in one expression, precedence alone does not save you:
console.log("Result: " + 10 + 5); // "Result: 105" (left-to-right concatenation)
console.log("Result: " + (10 + 5)); // "Result: 15" (parentheses force addition first)In the first line, both plus operators have equal precedence, so JavaScript evaluates them strictly left to right. It joins the string with 10 first, then joins that result with 5.
Add parentheses around the numbers to force addition before the string join, as shown in the second line.
Negation without parentheses
The NOT operator has very high precedence. Be careful when negating a comparison:
console.log(!5 > 3); // false: (!5) > 3, then false > 3, which is false
console.log(!(5 > 3)); // false: !(true), which is false (intent is clearer)NOT runs before the comparison, so it flips the number to false first, and only then does the greater-than check run against that false value. If you meant to negate the whole comparison, wrap it in parentheses so NOT applies to the comparison's result instead of to the number alone.
How to Never Get Precedence Wrong
You do not need to memorize the entire precedence table. Follow these three rules instead:
- Multiplication/division before addition/subtraction -- same as math class.
- AND before OR -- think of AND as multiplication and OR as addition of logic.
- When an expression has more than two operators, add parentheses. They make intent obvious and cost nothing.
This expression is technically correct without parentheses:
const valid = age >= 18 && hasLicense || isStaff;Both lines run identically because AND already binds tighter than OR by default. But the version below makes that grouping visible instead of relying on memorized precedence rules, which matters once other people start reading and maintaining your code:
const valid = (age >= 18 && hasLicense) || isStaff;The second version tells the reader "either you are an adult with a license, or you are staff." The first version makes the reader stop and think about precedence. Write for humans, not just for the JavaScript engine.
Rune AI
Key Insights
- Operator precedence determines which operation runs first in a multi-operator expression.
- Multiplication and division run before addition and subtraction, just like in math.
- Comparison operators run before logical operators.
- AND (&&) runs before OR (||).
- Parentheses ( ) always override the default precedence.
- When in doubt, use parentheses to make the order explicit.
Frequently Asked Questions
Does JavaScript follow PEMDAS like math class?
How do I remember operator precedence?
What is operator associativity?
Conclusion
Operator precedence is the rule that decides which operation JavaScript evaluates first in an expression with multiple operators. You do not need to memorize every level. Know that multiplication comes before addition, AND comes before OR, and parentheses always take priority. When an expression looks ambiguous, add parentheses to make it clear for both JavaScript and the next person reading your code.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.