Operator Precedence

Updated · 7 min read
easyES2026javascriptoperatorsprecedencebitwise

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.

PrecedenceOperatorsAssociativityExample
1 (highest)( ... ) groupingn/a(a + b) * c
2. ?. [] () call, new with argumentsLeft to rightobj.fees[0]
3x++ x--n/acount++
4! -x +x ++x --x typeof await deleteRight to left!isPaid, typeof x
5**Right to left2 ** 3 ** 2
6* / %Left to righta * b / c
7+ -Left to righta + b - c
8<< >> >>>Left to rightx << 2
9< <= > >= in instanceofLeft to righta < b
10== != === !==Left to righta === b
11&Left to rightflags & READ
12^Left to righta ^ b
13|Left to rightflags | WRITE
14&&Left to righta && b
15|| ??Left to righta || b
16? :Right to lefta ? b : c
17 (lowest)= += -= ??= and other assignments, =>Right to lefta = b = 0
, commaLeft to righta, 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.

precedence.js▶ Try it
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);
Output
180
300
3
512
true

Explanation:

  • 30 * 2 runs before +, so 120 + 60.
  • - is left to right: (10 - 4) - 3 is 3, not 10 - (4 - 3).
  • ** is right to left: 2 ** (3 ** 2) is 2 ** 9, which is 512.
  • Comparisons run before &&, and && before ||: (true && false) || true.

Associativity in Assignment

Assignment is right to left, which is why chained assignment works:

assignment-chain.js▶ Try it
let paid, owed;
paid = owed = 0;
console.log(paid, owed);
Output
0 0

owed = 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 (a SyntaxError otherwise).
parens.js▶ Try it
const balance = 80;
const isPaid = false;
 
console.log("Total: " + isPaid ? 0 : balance);
console.log("Total: " + (isPaid ? 0 : balance));
Output
0
Total: 80

Explanation: 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.

OperatorNameExampleResult
a & bAND: 1 where both bits are 15 & 3 (101 & 011)1
a | bOR: 1 where either bit is 15 | 37
a ^ bXOR: 1 where bits differ5 ^ 36
~aNOT: flips every bit~5-6
a << nShift left (multiply by 2 per step)5 << 110
a >> nShift right, keeping the sign-10 >> 1-5
a >>> nShift right, filling with zeros-1 >>> 2815

The classic use is flags: storing several yes/no settings in one number.

flags.js▶ Try it
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));
Output
true
false
11

Common 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".

practice.js
console.log(100 + 50 * 2);
const isPaid = false;
console.log("Due: " + isPaid ? 0 : 120);
Show solution
practice-solution.js▶ Try it
console.log((100 + 50) * 2);
const isPaid = false;
console.log("Due: " + (isPaid ? 0 : 120));
Output
300
Due: 120

Quick check: Operator precedence

3 questions
1What is 2 + 3 * 4?
2Which operator is right-to-left associative?
3In a || b && c, which runs first?

Key Takeaways

RuleExample
* before +1 + 2 * 3 is 7
&& before ||a || b && c is a || (b && c)
** right to left2 ** 3 ** 2 is 512
Conditional is lowwrap it: "x" + (c ? a : b)

Was this tutorial helpful?

Lesson 11 of 139