Skip to content

operator precedence

Operator precedence decides how JavaScript groups the operators in an expression that has no parentheses, so 3 + 10 * 2 evaluates to 23 because * takes its operands before + does.

When two operators share a precedence level, associativity breaks the tie. Most binary operators group from the left, so a - b - c means (a - b) - c. Exponentiation, assignment, and the ternary operator group from the right. The grouping operator ( ) overrides both rules, because a parenthesized expression is evaluated as one operand.

How JavaScript Operator Precedence Works

ECMA-262 encodes precedence in its expression grammar, and MDN ranks the result in its operator precedence table from level 18 for grouping down to level 1 for the comma operator. A few rows of that table cause most precedence bugs:

Expression Parsed as Rule
"Total: " + 100 + 5 ("Total: " + 100) + 5 + groups from the left
2 ** 3 ** 2 2 ** (3 ** 2) ** groups from the right
flags & 2 === 2 flags & (2 === 2) === ranks above &
!key in config (!key) in config ! ranks above in
a = b = 0 a = (b = 0) = groups from the right
-2 ** 2 SyntaxError A unary operator can’t sit to the left of **
a ?? b || c SyntaxError ?? can’t mix with || or && without parentheses

The last two rows aren’t parsed at all. The grammar rejects both forms, so the code has to state the grouping with parentheses.

Precedence only decides which operator gets which operands. It doesn’t change the order in which operands run. JavaScript evaluates operands from left to right, so in a() + b() * c() the call to a() happens first, even though the multiplication is applied before the addition.

JavaScript Operator Precedence Examples

A checkout page builds a label from a price and a shipping fee. Because + groups from the left, the string joins with the first number before the two numbers can be added:

Language: JavaScript
> const price = 100;
undefined
> const shipping = 5;
undefined

> "Total: " + price + shipping
'Total: 1005'

> "Total: " + (price + shipping)
'Total: 105'

The first expression concatenates twice, as the arithmetic operators entry explains for + with a string operand. Grouping the sum makes the addition run on two numbers first.

A permission check tests whether the write bit, 2, is set in a bitmask. The strict equality operator ranks above bitwise AND, so the unparenthesized check compares 2 === 2 first:

Language: JavaScript
> const permissions = 6;
undefined

> permissions & 2 === 2
0

> (permissions & 2) === 2
true

The first expression computes 6 & true. Bitwise AND converts true to 1, and the lowest bit, 1, isn’t set in 6, so the result is 0 even though the write bit is set. Parentheses make the comparison see the masked bit.

JavaScript Operator Precedence Gotchas

  • The nullish coalescing operator (??) shares a level with ||, but mixing the two without parentheses is a SyntaxError.
  • Writing -2 ** 2 throws a SyntaxError, so the code must choose (-2) ** 2, which is 4, or -(2 ** 2), which is -4.
  • An unparenthesized !key in config applies ! to the key before the in test runs, so it checks for a property named "false" or "true".
  • Parentheses never change operand order, so a side effect in a left operand still runs before one in a right operand.

JavaScript Operator Precedence Use Cases

  • Predicting the result of an expression that mixes strings and numbers
  • Deciding where parentheses are required around ??, **, and bitwise checks
  • Reviewing generated code for conditions that group differently than they read

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026