For example, consider the expression
3+5*2. If you calculate from left to right, the answer you get is
16). However, if you perform the multiplication first and then the addition, the result is
13). In other words, a single expression can produce multiple answers depending on the order in which you perform the calculations.
The order of precedence
|Operator||Operation||Order of Precedence||Order of Evaluation|
||Increment||First||R -> L|
||Decrement||First||R -> L|
||Negation||First||R -> L|
||NOT||First||R -> L|
||Multiplication, division, modulus||Second||L -> R|
||Addition, subtraction||Third||L -> R|
||Concatenation||Third||L -> R|
||Less than, less than, or equal||Fourth||L -> R|
||Greater than, greater than, or equal||Fourth||L -> R|
||Equal||Fifth||L -> R|
||Not equal||Fifth||L -> R|
||Identity||Fifth||L -> R|
||Non-identity||Fifth||L -> R|
||AND||Sixth||L -> R|
||OR||Sixth||L -> R|
||Ternary||Seventh||R -> L|
||Assignment||Eighth||R -> L|
||Arithmetic assignment||Eighth||R -> L|
=3+5*2 (just discussed) is
5*10/2. If you perform the multiplication first, the answer you get is
25). If you perform the division first, you also get an answer of
Controlling the order of precedence
Sometimes you want to take control of the situation and override the order of precedence. That might seem like a decidedly odd thing to do, so perhaps an example is in order. As you probably know, you calculate the total cost of a retail item by multiplying the retail price by the tax rate, and then adding that result to the retail price:
Total Price = Retail Price + Retail Price * Tax Rate
However, what if you want to reverse this calculation? That is, suppose you know the final price of an item and, given the tax rate, you want to know the original (that is, pre-tax) price. Applying a bit of algebra to the preceding equation, it turns out that you can calculate the original price by dividing the total price by 1 plus the tax rate. So if the total price is $11.00 and the tax rate is 10%, then you divide 11 by 1.1 and get an answer of $10.00.
retailPrice = totalPrice / 1 + taxRate;
The following code implements this formula:
var totalPrice = 11.00; var taxRate = .1; var retailPrice = totalPrice / 1 + taxRate; alert("The pre-tax price is " + retailPrice);
totalPrice value first is divided by 1 and then is added to the
taxRate value, which isn’t the correct order.
To get the correct answer, you have to override the order of precedence so that the addition
1 + taxRate is performed first. You override precedence by surrounding that part of the expression with parentheses, as shown in the following code. Using this revised script, you get the correct answer.
var totalPrice = 11.00; var taxRate = .1; var retailPrice = totalPrice / (1 + taxRate); alert("The pre-tax price is " + retailPrice);
One of the most common mistakes when using parentheses in expressions is to forget to close a parenthetic term with a right parenthesis. To make sure you’ve closed each parenthetic term, count all the left parentheses and count all the right parentheses. If these totals don’t match, you know you’ve left out a parenthesis.