JavaScript Operator Precedence, Associativity and Evaluation Order
How JavaScript decides what an expression with several operators means: operand counts, lvalues, side effects, the precedence table, left and right associativity, why -2 ** 2 and a ?? b || c are syntax errors, and the strict left-to-right order in which operands are evaluated.
kvi5hnuCode Sprout team member8 min readOn this page
What is 2 + 3 * 4? You know it's 14, not 20, because multiplication comes first. JavaScript has about 50 operators, and the same question arises for all of them: in !user.active && count > 0, does ! apply to the comparison or to user.active? Three separate rules answer it:
- Precedence: which operators bind more tightly when different operators meet.
- Associativity: how operators of the same precedence group (
a - b - c). - Evaluation order: in what order the operands are actually computed. JavaScript always goes left to right.
The first two decide the shape of the expression; the third decides the timing. This article covers all three, plus the vocabulary used in the rest of Part 4.

Operator vocabulary
Number of operands. Most operators are binary: they combine two operands (a * b). Unary operators take one (-x, !done, typeof v, ++i). There's exactly one ternary operator, the conditional cond ? a : b.
Symbols and keywords. Most operators are punctuation (+, ===, &&), but some are keywords: typeof, instanceof, in, delete, void, new and await. They're ordinary operators with longer names.
Operand types. Operators expect certain types and convert what they get (article 23). * wants numbers, so "6" * "2" is 12. + works on numbers or strings, which is why it's special (article 31). ! works on any value and returns a boolean.
lvalues. Some operators need an operand that can be assigned to: a variable, a property (obj.x, obj["x"]) or an array element (arr[0]). The historical name is lvalue ("left value", what can appear on the left of =). Assignment, ++, -- and delete need one. 5 = x or (a + b)++ is a SyntaxError.
Side effects. Most operators just compute a value. A few also change state: assignment operators (=, +=, ...), ++ and --, and delete. Calls and new can have any side effect the function performs. Side effects are why evaluation order matters, as you'll see at the end.
Precedence
Higher-precedence operators bind more tightly, as if they had invisible parentheses around them. Here's the full order, from tightest to loosest. You don't need to memorize it, but scanning it once will save you from some classic bugs:
| Level | Operators | Associativity |
|---|---|---|
| 1 (tightest) | ( … ) grouping | n/a |
| 2 | ., ?., [ ], calls f(), new X() with arguments | left |
| 3 | new X without arguments | right |
| 4 | postfix x++, x-- | n/a |
| 5 | !, ~, unary + and -, prefix ++x and --x, typeof, void, delete, await | right |
| 6 | ** | right |
| 7 | *, /, % | left |
| 8 | +, - | left |
| 9 | <<, >>, >>> | left |
| 10 | <, <=, >, >=, in, instanceof | left |
| 11 | ==, !=, ===, !== | left |
| 12 | & | left |
| 13 | ^ | left |
| 14 | | | left |
| 15 | && | left |
| 16 | ||, ?? (can't be mixed without parentheses) | left |
| 17 | ? :, =, += and all other assignments, =>, yield, spread ... | right |
| 18 (loosest) | , comma | left |
The rules of thumb that matter in daily code:
- Property access and calls bind tightest, so
typeof user.getAge()istypeof (user.getAge()). - Unary operators beat binary ones.
- Arithmetic comes before comparison, comparison before equality, and equality before logical
&&/||. That's whya + 1 > b && c === dneeds no parentheses. &&binds tighter than||.- Assignment and the ternary operator come almost last, so
x = a > b ? a : bassigns the result of the whole conditional.
console.log(2 + 3 * 4, (2 + 3) * 4);
console.log(typeof 1 + 2); // (typeof 1) + 2
console.log(true || false && false); // true || (false && false)
console.log((true || false) && false);
14 20
number2
true
false
Unary operators bind very tightly
Because unary operators sit high in the table, they grab only the operand right next to them. Two traps follow:
const obj = { key: 1 };
console.log(!"key" in obj); // (!"key") in obj → false in obj → "false" in obj
console.log(!("key" in obj)); // what was meant
const list = { items: [] };
console.log(!list instanceof Array); // (!list) instanceof Array → false instanceof Array
console.log(!(list instanceof Array));
false
false
false
true
The first and third lines look sensible, return false, and are silently wrong: they'd return false whatever obj and list held. Always wrap the test in parentheses when negating in or instanceof. ESLint's no-unsafe-negation rule (on in the recommended preset) flags this.
Two combinations are syntax errors on purpose
Where a reader would have to guess, JavaScript refuses to guess:
for (const code of ["-2 ** 2", "null ?? 0 || 5"]) {
try {
eval(code);
} catch (err) {
console.log(code.padEnd(15), err.name);
}
}
console.log((-2) ** 2, -(2 ** 2));
console.log((null ?? 0) || 5, null ?? (0 || 5));
-2 ** 2 SyntaxError
null ?? 0 || 5 SyntaxError
4 -4
5 5
- Unary minus with
**: mathematicians read-2²as-(2²) = -4, while some languages compute(-2)² = 4. JavaScript makes you write the parentheses. ??with&&or||:??was added in ES2020, and the committee chose to require parentheses rather than pick a precedence that half of readers would get wrong.
Associativity
When operators of the same precedence meet, associativity decides how they group. Most binary operators are left-associative: a - b - c means (a - b) - c.
A few groups are right-associative: **, unary operators, assignment and the conditional operator.
console.log(10 - 4 - 3, 64 / 4 / 2); // (10 - 4) - 3, (64 / 4) / 2
console.log(2 ** 3 ** 2); // 2 ** (3 ** 2) = 2 ** 9
let a, b, c;
a = b = c = 7; // a = (b = (c = 7))
console.log(a, b, c);
const grade = (score) => score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade(95), grade(85), grade(40)); // chained conditionals group to the right
3 8
512
7 7 7
A B C
Chained assignment works because c = 7 is an expression whose value is 7, which is then assigned to b, and so on.
Left associativity with mixed types
Left-to-right grouping plus +'s double meaning gives results that depend on the order of operands:
console.log(4 + 3 + "px"); // (4 + 3) + "px" → "7px"
console.log("px" + 4 + 3); // ("px" + 4) + 3 → "px43"
console.log(1 < 2 < 3); // (1 < 2) < 3 → true < 3 → 1 < 3
console.log(3 > 2 > 1); // (3 > 2) > 1 → true > 1 → 1 > 1
7px
px43
true
false
1 < 2 < 3 being true is an accident; 3 > 2 > 1 being false shows it doesn't test a range. Write a < b && b < c.
Evaluation order: always left to right
Precedence and associativity decide the shape of the tree, but not when each operand is computed. JavaScript evaluates operands strictly left to right. In v(1) + v(2) * v(3), the multiplication happens first, but v(1) is called first:
const calls = [];
const v = (n) => { calls.push(n); return n; };
const result = v(1) + v(2) * v(3);
console.log(result, calls.join(" "));
7 1 2 3
The engine evaluates v(1), then v(2), then v(3), then multiplies 2 by 3, then adds 1. Parentheses change which operations combine, never the left-to-right order of the operands.
This includes assignment: the target on the left is evaluated before the value on the right.
let i = 0;
const slots = [];
slots[i] = ++i; // the target slots[0] is fixed before ++i runs
console.log(slots, i);
let k = 1;
const total = k++ + k; // 1 (then k becomes 2) + 2
console.log(total, k);
[ 1 ] 1
3 2
Order of evaluation only matters when operands have side effects that affect each other, as in these examples. Code that depends on such subtle ordering is hard to read, even though its behavior is well-defined. Split it into separate statements.
Short-circuiting skips evaluation entirely
&&, ||, ??, ?. and the conditional operator are the exceptions to "every operand is evaluated": they may skip their right side completely (articles 29 and 34):
const seen = [];
const w = (n) => { seen.push(n); return n; };
const out = w(1) || w(2) && w(3); // 1 || (2 && 3): the left side is truthy
console.log(out, seen.join(" "));
1 1
w(2) and w(3) never run. Even though && has higher precedence, it is the right operand of ||, and || already has its answer.
The comma operator
The comma operator has the lowest precedence of all. It evaluates its left operand, discards the result, then evaluates and returns its right operand:
const last = (1, 2, 3);
console.log(last);
for (let lo = 0, hi = 4; lo < hi; lo++, hi--) { // lo++, hi-- is one expression
console.log(lo, hi);
}
3
0 4
1 3
Its main legitimate use is the update part of a for loop. Note that the commas in let lo = 0, hi = 4, in function arguments and in array literals are separators, not the comma operator.
Common questions
Should I memorize the precedence table?
No. Know the rules of thumb above, and use parentheses whenever a reader might hesitate, even if they're technically redundant. (a && b) || c is clearer than a && b || c.
Does a && b || c work as a ternary a ? b : c?
Only when b is truthy. If a is true and b is falsy (like 0 or ""), a && b is falsy and you get c. Use the real conditional operator.
Is x++ + ++x defined behavior in JavaScript?
Yes. Unlike C, where such code is undefined behavior, JavaScript's strict left-to-right evaluation makes it deterministic. With x = 1: x++ gives 1 (x becomes 2), ++x gives 3, total 4. It's still terrible code: nobody should have to work that out.
Why is await in the unary group?
await promise is a unary operator. Its high precedence means await a + b is (await a) + b, and await getUser().name is await (getUser().name), reading .name of the promise. Write (await getUser()).name. Part 13 covers await.
Summary
- Precedence decides how different operators group: access and calls, then unary,
**,* / %,+ -, shifts, comparisons, equality, bitwise,&&,|| ??, then assignment/conditional, then comma. - Associativity groups equal-precedence operators: left for most, right for
**, unary, assignment and?:. -2 ** 2and mixing??with&&/||are syntax errors that force you to add parentheses.- Negating
in/instanceofneeds parentheses:!(key in obj). - Evaluation order is always left to right, including the assignment target before the value; only short-circuiting operators skip operands.
- When in doubt, add parentheses; when side effects interact, split the statement.
Practice Questions
What is 3 + 4 * 2 ** 2?
Answer
**first (2 ** 2 = 4), then*(4 * 4 = 16), then+(3 + 16).
What does typeof typeof 42 evaluate to?
Answer
"string". Unary operators are right-associative: typeof 42 is "number", and typeof "number" is "string".
Is this code valid?
const limit = settings.max || 10 ?? 0;
Answer
No. Mixing || and ?? without parentheses is a SyntaxError. Write (settings.max || 10) ?? 0, or, more likely what was meant, settings.max ?? 10.
What does console.log(log.join("")) print after this code runs?
const log = [];
const f = (x) => (log.push(x), x);
f("a") + f("b") * f("c");
Answer
abc. Operands are evaluated left to right; precedence only decides that "b" * "c" is combined before the addition.
Why does if (!user.roles instanceof Array) never run its body?
Answer
It parses as (!user.roles) instanceof Array. !user.roles is a boolean, and a boolean is never an instance of Array, so the condition is always false. Write !(user.roles instanceof Array), or better, !Array.isArray(user.roles).
Previous: JavaScript Expressions: Primary Expressions, Literals, Initializers and Property Access
Next: JavaScript Arithmetic Operators: +, -, *, /, %, **, Increment and Decrement

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.