JavaScript Operator Precedence, Associativity and Evaluation
Quick answer
Precedence determines how operators group; associativity resolves grouping among operators of the same precedence. Neither alone specifies every runtime side effect.
How it works
Multiplication binds before addition, while exponentiation ** groups to the right. Parentheses override grouping. Unary plus converts to Number and unary minus negates a numeric value; the comma operator evaluates left then right and yields the right value. Operand evaluation normally proceeds left to right for binary expressions even if a later operator binds more tightly; short-circuit operators may omit an operand. A unary minus directly before the left operand of exponentiation is forbidden without parentheses. Use (-2) ** 2 or -(2 ** 2) to distinguish the intentions.
console.log(2 + 3 * 4);console.log((2 + 3) * 4);console.log(2 ** 3 ** 2);Expected output in Node.js:
1420512Exponentiation groups as 2 ** (3 ** 2).
function read(n) { console.log(n); return n; }console.log(read(1) + read(2) * read(3));Expected output in Node.js:
1237The calls run left to right even though multiplication combines its values first.
Common mistakes
Do not infer evaluation order solely from precedence. Avoid relying on subtle grouping for code with side effects; use named intermediate results.
Practice
Add parentheses to the first example to make each grouping explicit, then predict the call order in the second.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Operator Precedence, Associativity and Evaluation — Knowledge Check
Apply the taught concept and reason about the result.
What is 2 + 3 * 4?
What is (2 + 3) * 4?
What is 2 ** 3 ** 2?
Which is valid syntax for squaring negative two?
In read(1) + read(2) * read(3), which call occurs first?