Sub topic 2
Who Goes First: Precedence and Associativity
This chapter assumes you can declare variables with let and const, write and call functions, and run code in a browser console or Node.js. It covers how JavaScript decides which operator applies first when an expression contains several of them.
Precedence: Which Operator Binds Tighter
Precedence is a ranking of operators. When two different operators compete for the same operand, the one with higher precedence takes it first. Multiplication ranks above addition, so 3 * 4 is grouped before the + is applied.
console.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20Parentheses form a grouping, and a grouping overrides every precedence rule. If you have to stop and think about how an expression groups, add parentheses so the next reader doesn't have to.
Here is a rough ranking of common operators, from tightest to loosest:
| Rank | Operators | Associativity | Column | Column |
|---|---|---|---|---|
| Highest | `( )` grouping, `.`, `?.`, calls | left | ||
| `!`, unary `-`, unary `+`, `typeof` | n/a | |||
| `**` | right | |||
| `*`, `/`, `%` | left | |||
| `+`, `-` | left | |||
| `<`, `>`, `<=`, `>=` | left | |||
| `==`, `===`, `!=`, `!==` | left | |||
| `&&`, then `\ | \ | `, then `??` | left | |
| Lowest | `=`, `+=`, `??=` and other assignments | right |
Associativity: Breaking Ties
When the operators next to an operand have equal precedence, associativity decides the grouping. Most binary operators are left-associative, so they group from the left. Exponentiation and assignment are right-associative.
console.log(10 - 4 - 3); // 3, grouped as (10 - 4) - 3
console.log(100 / 10 / 2); // 5, grouped as (100 / 10) / 2
console.log(2 ** 3 ** 2); // 512, grouped as 2 ** (3 ** 2)
let a, b;
a = b = 5; // grouped as a = (b = 5)
console.log(a, b); // 5 5Common Errors
Chained comparisons
Comparisons are left-associative, so 3 > 2 > 1 does not mean what it does in math:
console.log(3 > 2 > 1); // false: (3 > 2) is true, then true > 1 becomes 1 > 1
console.log(3 > 2 && 2 > 1); // trueUnary minus before `**`
Some languages read -2 ** 2 as -(2 ** 2) and others as (-2) ** 2. JavaScript refuses to guess and throws a SyntaxError, so you have to add parentheses:
console.log((-2) ** 2); // 4
console.log(-(2 ** 2)); // -4
// console.log(-2 ** 2); // SyntaxErrorconst log = (n) => { console.log('called', n); return n; };
console.log(log(1) + log(2) * log(3)); // called 1, called 2, called 3, then 7Exercise
What does 2 * 3 ** 2 - 4 / 2 evaluate to, and how is it grouped?
Show answer
16. ** binds tightest, so 3 2 = 9. Next come and /, which give 2 9 = 18 and 4 / 2 = 2. The subtraction runs last: 18 - 2 = 16. The full grouping is (2 * (3 2)) - (4 / 2).
Exercise
Why does 1 < 2 < 3 return true while 3 > 2 > 1 returns false?
Show answer
Both group from the left. In 1 < 2 < 3, the first comparison gives true, and true < 3 becomes 1 < 3, which is true. In 3 > 2 > 1, the first comparison gives true, and true > 1 becomes 1 > 1, which is false. The first result is correct only by luck.