JavaScript Reference ยท Operators
Operator precedence
Reference table of JavaScript operator precedence and associativity from grouping to comma, with live examples of common surprises and parenthesis rules.
Operator precedence decides which operators bind tighter when an expression has several of them without parentheses. In 2 + 3 * 4 the * wins, so it behaves like 2 + (3 * 4). When two operators have the same precedence, associativity decides: left-to-right for most (a - b - c is (a - b) - c) and right-to-left for assignment, ** and the conditional operator.
Precedence affects how the expression is grouped, not the order in which operands are evaluated. Operands are still evaluated left to right, and short-circuiting operators such as &&, ||, ?? and ?. skip evaluating the right side when they can.
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The table
From highest precedence (binds tightest) to lowest. Operators on the same row share a precedence.
| Level | Operators | Associativity |
|---|---|---|
| 18 | ( ) grouping |
n/a |
| 17 | member access a.b, a[b]; optional chaining a?.b; call f(); new X() with arguments; import.meta, new.target |
left to right |
| 16 | new X without arguments |
n/a |
| 15 | postfix x++, x-- |
n/a |
| 14 | prefix ++x, --x; unary !, ~, +, -; typeof, void, delete, await |
right to left |
| 13 | ** |
right to left |
| 12 | *, /, % |
left to right |
| 11 | +, - |
left to right |
| 10 | <<, >>, >>> |
left to right |
| 9 | <, <=, >, >=, in, instanceof |
left to right |
| 8 | ==, !=, ===, !== |
left to right |
| 7 | & |
left to right |
| 6 | ^ |
left to right |
| 5 | | |
left to right |
| 4 | && |
left to right |
| 3 | ||, ?? |
left to right |
| 2 | ? : conditional; => arrow; assignment =, +=, &&=, ||=, ??= and friends; yield, yield* |
right to left |
| 1 | , comma |
left to right |
| 0 | spread ... (not a real operator) |
n/a |
The exact level numbers vary between sources; only the relative order matters.
Usage notes
- Unary minus and
**.-2 ** 2is aSyntaxErrorto avoid ambiguity. Write(-2) ** 2(4) or-(2 ** 2)(-4). ??with||or&&. Mixing them without parentheses is aSyntaxError, even though??sits at the same level as||.- Comparison chains do not do what math does.
1 < x < 3is(1 < x) < 3, a boolean compared with a number. Write1 < x && x < 3. typeofandawaitare unary.typeof x === "string"works as expected, buttypeof x + yis(typeof x) + y.- Arrow function bodies extend as far as an assignment expression can, but stop at a comma: in
[x => x + 1, 2]the body isx + 1. - Increment pitfalls.
i++ + ++iis legal but hard to read. Avoid combining several side effects in one expression. - When in doubt, add parentheses. They cost nothing and make the intent obvious to readers and linters.
Browser support
Precedence is part of the language grammar and is the same in every engine. Newer operators (??, ?., **, logical assignment) simply need an engine that supports them; check a compatibility table for very old targets.