Operator Precedence
Operator precedence is the set of rules that decides which operator runs first when an expression has several. In 120 + 30 * 2, multiplication has higher precedence than addition, so the result is 180, not 300. Associativity decides the order when operators have the same precedence: most run left to right, a few right to left. Knowing the main rules lets you read code correctly; knowing when to add parentheses lets others read yours.
In One Minute
- Higher precedence runs first:
*before+, comparisons before&&,&&before||. - Same precedence: most operators go left to right;
**, assignment and?:go right to left. - Parentheses override everything. When in doubt, add them.
- Bitwise operators (
&,|,^,~,<<,>>,>>>) work on the bits of 32-bit integers. They are optional for most programs.
The Precedence Table
From highest (runs first) to lowest. Operators in the same row have the same precedence.
| Precedence | Operators | Associativity | Example |
|---|---|---|---|
| 1 (highest) | ( ... ) grouping | n/a | (a + b) * c |
| 2 | . ?. [] () call, new with arguments | Left to right | obj.fees[0] |
| 3 | x++ x-- | n/a | count++ |
| 4 | ! -x +x ++x --x typeof await delete | Right to left | !isPaid, typeof x |
| 5 | ** | Right to left | 2 ** 3 ** 2 |
| 6 | * / % | Left to right | a * b / c |
| 7 | + - | Left to right | a + b - c |
| 8 | << >> >>> | Left to right | x << 2 |
| 9 | < <= > >= in instanceof | Left to right | a < b |
| 10 | == != === !== | Left to right | a === b |
| 11 | & | Left to right | flags & READ |
| 12 | ^ | Left to right | a ^ b |
| 13 | | | Left to right | flags | WRITE |
| 14 | && | Left to right | a && b |
| 15 | || ?? | Left to right | a || b |
| 16 | ? : | Right to left | a ? b : c |
| 17 (lowest) | = += -= ??= and other assignments, => | Right to left | a = b = 0 |
, comma | Left to right | a, b |
You don't need to memorize it. Remember the big picture: member access and calls, then unary, then math (power, multiply, add), then comparison, then equality, then logic (&& before ||), then the conditional, then assignment.
console.log(120 + 30 * 2);
console.log((120 + 30) * 2);
console.log(10 - 4 - 3);
console.log(2 ** 3 ** 2);
console.log(100 > 50 && 20 > 30 || true);180
300
3
512
trueExplanation:
30 * 2runs before+, so120 + 60.-is left to right:(10 - 4) - 3is3, not10 - (4 - 3).**is right to left:2 ** (3 ** 2)is2 ** 9, which is512.- Comparisons run before
&&, and&&before||:(true && false) || true.
Associativity in Assignment
Assignment is right to left, which is why chained assignment works:
let paid, owed;
paid = owed = 0;
console.log(paid, owed);0 0owed = 0 runs first and its value (0) is then assigned to paid. Chained assignment is legal but easy to misread; two separate lines are clearer.
When to Use Parentheses
Parentheses cost nothing at run time and save readers from looking up the table. Add them when:
- An expression mixes
&&and||:(isAdmin || isOwner) && !isLocked. - An expression mixes arithmetic with comparisons in a long line.
- You use the conditional operator inside a bigger expression:
"Total: " + (isPaid ? 0 : balance). - You mix
??with&&or||, where JavaScript requires them (aSyntaxErrorotherwise).
const balance = 80;
const isPaid = false;
console.log("Total: " + isPaid ? 0 : balance);
console.log("Total: " + (isPaid ? 0 : balance));0
Total: 80Explanation: without parentheses, + runs before ?:. The expression becomes ("Total: " + false) ? 0 : balance. The string "Total: false" is truthy, so the whole thing is 0. The parentheses make the conditional run first.
Bitwise Operators (Optional)
Bitwise operators work on numbers as 32-bit integers, bit by bit. Most applications never need them; you'll meet them in permission flags, low-level data formats and older code.
| Operator | Name | Example | Result |
|---|---|---|---|
a & b | AND: 1 where both bits are 1 | 5 & 3 (101 & 011) | 1 |
a | b | OR: 1 where either bit is 1 | 5 | 3 | 7 |
a ^ b | XOR: 1 where bits differ | 5 ^ 3 | 6 |
~a | NOT: flips every bit | ~5 | -6 |
a << n | Shift left (multiply by 2 per step) | 5 << 1 | 10 |
a >> n | Shift right, keeping the sign | -10 >> 1 | -5 |
a >>> n | Shift right, filling with zeros | -1 >>> 28 | 15 |
The classic use is flags: storing several yes/no settings in one number.
const CAN_VIEW = 1; // binary 001
const CAN_EDIT = 2; // binary 010
const CAN_REFUND = 4; // binary 100
let permissions = CAN_VIEW | CAN_EDIT; // 011
console.log((permissions & CAN_EDIT) !== 0);
console.log((permissions & CAN_REFUND) !== 0);
console.log(permissions.toString(2));true
false
11Common Mistakes
String joining before a conditional. "Total: " + isPaid ? a : b doesn't do what it looks like. Wrap the conditional in parentheses.
Assuming - or / are right to left. 100 / 10 / 2 is 5, not 20.
Forgetting that ! binds tightly. !fee > 100 means (!fee) > 100, which compares a boolean with 100. Write !(fee > 100) or, better, fee <= 100.
Writing -2 ** 2. It is a SyntaxError, because it is unclear whether you mean (-2) ** 2 or -(2 ** 2). JavaScript makes you choose with parentheses.
Practice
Challenge: Predict each result, then add parentheses so that the first line prints 300 and the second prints "Due: 120".
console.log(100 + 50 * 2);
const isPaid = false;
console.log("Due: " + isPaid ? 0 : 120);Show solution
console.log((100 + 50) * 2);
const isPaid = false;
console.log("Due: " + (isPaid ? 0 : 120));300
Due: 120Key Takeaways
| Rule | Example |
|---|---|
* before + | 1 + 2 * 3 is 7 |
&& before || | a || b && c is a || (b && c) |
** right to left | 2 ** 3 ** 2 is 512 |
| Conditional is low | wrap it: "x" + (c ? a : b) |
Was this tutorial helpful?