Operator precedence and associativity
Operator precedence decides which operator in an expression runs first, and associativity decides the order when operators share the same precedence. Together they explain why 2 + 3 * 4 is 14 rather than 20, and why some expressions give results you did not expect. When in doubt, parentheses override both rules.
Precedence: who goes first
Operators with higher precedence bind more tightly, so they are evaluated before lower ones. Multiplication beats addition, comparison beats logical AND, and AND beats OR. Here is a simplified order from highest to lowest:
| Level | Operators | Associativity |
|---|---|---|
| Grouping | ( ) | n/a |
| Member access, calls | . [] () ?. | left to right |
| Postfix / prefix | x++, ! typeof -x ++x | n/a / right to left |
| Exponent | ** | right to left |
| Multiplicative | * / % | left to right |
| Additive | + - | left to right |
| Relational | < > <= >= instanceof in | left to right |
| Equality | == != === !== | left to right |
| Logical AND | && | left to right |
| Logical OR, nullish | || ?? | left to right |
| Conditional | ? : | right to left |
| Assignment | = += -= and friends | right to left |
Associativity: same level, which side first
Most operators are left-associative: 10 - 4 - 3 is read as (10 - 4) - 3, which equals 3. A few are right-associative. Exponentiation is one: 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 512, not 64. Assignment is another, which is why a = b = 5 sets b first and then copies the result into a.
console.log(2 + 3 * 4); // 14 (* before +)
console.log((2 + 3) * 4); // 20 (parentheses first)
console.log(2 + 3 * 4 ** 2); // 50 (** then * then +)
console.log(2 ** 3 ** 2); // 512 (right to left)
console.log(10 - 4 - 3); // 3 (left to right)
const age = 20, member = false;
console.log(age > 18 && member || age > 60); // false
// read as: ((age > 18) && member) || (age > 60)
console.log(1 + 2 + "3"); // "33" (3 + "3")
console.log("1" + 2 + 3); // "123" (string from the start)
Precedence is not evaluation order
A subtle point: precedence decides how operators are grouped, not which operand is computed first. JavaScript always evaluates operands from left to right. In a() + b() * c(), the three functions are called in the order a, b, c, and only then is the multiplication done before the addition. Short-circuit operators can skip evaluating the right side entirely.
Expressions that catch people out
Mixing strings and numbers is the most common surprise. Because + is left-associative, 1 + 2 + "3" adds the numbers first and gives "33", while "1" + 2 + 3 becomes string concatenation from the very first step and gives "123".
JavaScript also refuses some ambiguous combinations outright. -2 ** 2 is a syntax error, because it is unclear whether you mean (-2) ** 2 or -(2 ** 2); you must add brackets. Likewise, mixing ?? with || or && without parentheses throws a syntax error.
Another classic is !a instanceof B, which is read as (!a) instanceof B and is almost always false. Write !(a instanceof B).
A practical rule
You do not need to memorise the whole table. Know that maths follows normal school rules, that AND binds tighter than OR, and that assignment comes last. For everything else, add parentheses. They cost nothing and make your intent obvious to the next reader.
Takeaway: precedence groups operators, associativity breaks ties at the same level, operands still run left to right, and parentheses are the cleanest way to remove any doubt.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.