Operator Precedence Reference
Operator precedence tables for JavaScript, Python, and C (highest to lowest precedence).
Back to all tools on ToolForge
About Operator Precedence Reference
This operator precedence reference provides side-by-side tables for JavaScript, Python, and C so you can quickly verify expression evaluation order and associativity.
Use it to debug ambiguous expressions, avoid precedence bugs, and improve readability with explicit parentheses where needed.
Quick Example: In
2 + 3 * 4, multiplication happens first: 2 + (3 * 4) = 14, not (2 + 3) * 4 = 20.
JavaScript Operator Precedence
| Level | Precedence | Operators | Associativity | Description |
|---|---|---|---|---|
| 1 | 21 | () [] . new ?. | L→R | Member access, function call |
| 2 | 20 | new (with args) | R→L | Object instantiation |
| 3 | 19 | ++ -- (postfix) | L→R | Postfix increment/decrement |
| 4 | 18 | ! ~ + - ++ -- typeof void delete | R→L | Unary operators |
| 5 | 17 | ** | R→L | Exponentiation |
| 6 | 16 | * / % | L→R | Multiplicative |
| 7 | 15 | + - | L→R | Additive |
| 8 | 14 | << >> >>> | L→R | Bitwise shifts |
| 9 | 13 | < > <= >= in instanceof | L→R | Relational |
| 10 | 12 | == != === !== | L→R | Equality |
| 11 | 11 | & | L→R | Bitwise AND |
| 12 | 10 | ^ | L→R | Bitwise XOR |
| 13 | 9 | | | L→R | Bitwise OR |
| 14 | 8 | && | L→R | Logical AND |
| 15 | 7 | || ?? | L→R | Logical OR / Nullish coalescing |
| 16 | 6 | ? : | R→L | Ternary conditional |
| 17 | 5 | = += -= *= /= %= &= |= ^= <<= >>= >>>= **= | R→L | Assignment operators |
| 18 | 4 | yield yield* | R→L | Generator yield |
| 19 | 3 | => | R→L | Arrow function |
| 20 | 1 | , | L→R | Comma (sequence) |
Python Operator Precedence
| Level | Precedence | Operators | Associativity | Description |
|---|---|---|---|---|
| 1 | 18 | () [] {} . | L→R | Grouping, indexing, attribute |
| 2 | 17 | ** | R→L | Exponentiation |
| 3 | 16 | +x -x ~x | R→L | Unary plus, minus, bitwise NOT |
| 4 | 15 | not | R→L | Logical NOT |
| 5 | 14 | * / // % @ | L→R | Multiplication, division, floor div, mod, matrix |
| 6 | 13 | + - | L→R | Addition, subtraction |
| 7 | 12 | << >> | L→R | Bitwise shifts |
| 8 | 11 | & | L→R | Bitwise AND |
| 9 | 10 | ^ | L→R | Bitwise XOR |
| 10 | 9 | | | L→R | Bitwise OR |
| 11 | 8 | in not in is is not < <= > >= != == | L→R | Comparisons, membership, identity |
| 12 | 7 | not | R→L | Boolean NOT |
| 13 | 6 | and | L→R | Boolean AND |
| 14 | 5 | or | L→R | Boolean OR |
| 15 | 4 | if else | R→L | Conditional expression |
| 16 | 3 | := | R→L | Walrus operator (assignment expression) |
| 17 | 2 | = += -= *= /= //= %= **= &= |= ^= <<= >>= | R→L | Augmented assignment |
| 18 | 1 | lambda | R→L | Lambda expression |
C Operator Precedence
| Level | Precedence | Operators | Associativity | Description |
|---|---|---|---|---|
| 1 | 15 | () [] -> . | L→R | Function call, array subscript, member access |
| 2 | 14 | ++ -- (postfix) () (cast) | R→L | Postfix increment/decrement, type cast |
| 3 | 13 | ++ -- (prefix) + - ! ~ * & sizeof _Alignof | R→L | Unary operators |
| 4 | 12 | * / % | L→R | Multiplicative |
| 5 | 11 | + - | L→R | Additive |
| 6 | 10 | << >> | L→R | Bitwise shifts |
| 7 | 9 | < <= > >= | L→R | Relational |
| 8 | 8 | == != | L→R | Equality |
| 9 | 7 | & | L→R | Bitwise AND |
| 10 | 6 | ^ | L→R | Bitwise XOR |
| 11 | 5 | | | L→R | Bitwise OR |
| 12 | 4 | && | L→R | Logical AND |
| 13 | 3 | || | L→R | Logical OR |
| 14 | 2 | ? : | R→L | Conditional (ternary) |
| 15 | 1 | = += -= *= /= %= <<= >>= &= ^= |= | R→L | Assignment |
| 16 | 0 | , | L→R | Comma operator |
Common Precedence Pitfalls
1. Bitwise AND vs Equality
// WRONG: & has LOWER precedence than == if (x & 1 == 0) // Evaluates as: x & (1 == 0) = x & 0 = 0 // CORRECT: Use parentheses if ((x & 1) == 0) // Checks if x is even
2. Logical AND vs Logical OR
// && binds tighter than || if (a || b && c) // Means: a || (b && c), NOT (a || b) && c // For clarity, use parentheses if (a || (b && c))
3. Assignment in Conditions
// C/JavaScript: = has very low precedence if (x = y && z) // Means: x = (y && z), assigns boolean! // Use == for comparison if (x == (y && z)) // Intentional assignment (C idiom) while ((c = getchar()) != EOF) // Read char until EOF
4. Python Chained Comparisons
# Python allows chained comparisons if 0 < x < 10: # Means: (0 < x) and (x < 10) # JavaScript requires explicit logic if (x > 0 && x < 10) // Cannot chain in JS
Operator Associativity Examples
| Expression | Associativity | Evaluates As | Result (a=1,b=2,c=3) |
|---|---|---|---|
a - b - c | L→R | (a - b) - c | -4 |
a ** b ** c | R→L | a ** (b ** c) | 1^8 = 1 |
a = b = c | R→L | a = (b = c) | All equal 3 |
a && b && c | L→R | (a && b) && c | true if all truthy |
a ? b : c ? d : e | R→L | a ? b : (c ? d : e) | Nested conditional |
Frequently Asked Questions
- What is operator precedence?
- Operator precedence is the default order a language uses to evaluate operators in an expression. Higher-precedence operators run first unless parentheses override the order.
- What is associativity in operators?
- Associativity decides direction when two operators have the same precedence. For example, subtraction is usually left-to-right, while assignment is usually right-to-left.
- Why does 2 + 3 * 4 equal 14 and not 20?
- Because multiplication has higher precedence than addition. The expression is read as
2 + (3 * 4). To force 20, write(2 + 3) * 4. - What is the precedence of logical operators?
- In most languages,
NOTbinds tighter thanAND, andANDbinds tighter thanOR. So!a && b || cis typically evaluated as((!a) && b) || c. - How does Python's precedence differ from JavaScript?
- Python and JavaScript are similar in many basics, but Python uses words like
and/or/notand supports chained comparisons such asa < b < c. JavaScript uses symbols like&&and||and does not support that comparison chain syntax the same way. - When should I use parentheses?
- Use parentheses whenever intent could be misread, especially with mixed arithmetic, bitwise, or logical operators. Even if optional, they make code easier to review and maintain.