What are JavaScript operators precedence and associativity

· Category: JavaScript

Short answer

Operator precedence determines which operator is evaluated first, while associativity determines the order for operators of the same precedence level.

How it works

Higher precedence operators bind tighter. Key levels (high to low):

  1. Grouping ()
  2. Member access . and [], function call ()
  3. Unary operators !, typeof, ++, --
  4. Multiplicative *, /, %
  5. Additive +, -
  6. Relational <, <=, >, >=
  7. Equality ==, ===
  8. Logical AND &&
  9. Logical OR ||
  10. Assignment = (right-associative)

Example

const a = 2 + 3 * 4;       // 14, not 20
const b = 10 - 4 - 2;      // 4 (left-associative)
const c = a = b = 1;       // c = 1 (right-associative)

Why it matters

Misunderstanding precedence causes silent bugs. Use parentheses to make intent explicit and improve readability, especially with mixed logical and arithmetic operators.