HomeToolsAbout a20k

Associativity and Precedence

Precedence

Precedence is how operators are parsed concerning each other

  • Operators with higher precedence become the operands of operators with lower precedence
    • because the result of a higher precedence operator is executed first, which (the result of that operation) becomes the operand of next operation

Associativity

Associativity determines how operators of the same precedence are grouped in the absence of parenthesis

operand1 (Operator1) operand2 (Operator2) operand3

If Operator1 and Operator2 have different precedence levels, the operator with the highest precedence goes first and associativity does not matter.

console.log(3 + 10 * 2); // 23 console.log(3 + (10 * 2)); // 23 because parentheses here are superfluous console.log((3 + 10) * 2); // 26 because the parentheses change the order

Left-to-Right vs Right-to-Left Associativity

© VincentVanKoh