Operator Precedence

Operator precedence describes the order in which operations are performed in an arithmetic expression. For example:
  var x = 100 + 50 * 3;  
Is the result the same as 150 * 3, or is it the same as 100 + 150? Is the addition or the multiplication done first? As in traditional school mathematics, the multiplication is done first. Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). So the value of x of this particular equation is 250.


And (as in school mathematics) the precedence can be changed by using parentheses. When using parentheses, the operations inside the parentheses are computed first:
  var x = (100 + 50) * 3;   (The value of x is 450)


When many operations have the same precedence (like addition and subtraction), they are computed from left to right:
  var x = 100 + 50 - 3;   (The value of x is 147)

Back Table of Contents Next