﻿ JavaScript Operators and Operator Preferences                   - Misty World

# JavaScript Operators and Operator Preferences

All of us are using mathematical basic operations very much in our lives. Adding, Subtracting, Dividing and etc. help us to act with numbers. In JavaScript like other programming languages we have these operations with specific operators.
An operator is a few characters that do a numerical, logical or other operations for us. In this lesson we want to illustrate numerical operators. The table below is sufficient for our aim:

 Operator Description Example Result + Adding one value to another (directly or by variables). a1= 3.5 a2= 4 + a1 a2 becomes 7.5 - Subtracting one value from another b1= 3.5 b2= 4 - a1 - .5 b2 becomes 0 * Multiplying one value by another c1= 2 c2= c1*c1 +1 c2 becomes 5 / Dividing one value by another d1= 90 d2= d1/10 + 1 d2 becomes 10 % Remained value from dividing of it by another value e1= 7 e2= d1 % 4 e2 becomes 3 ++ Add one to a variable f= 8 f++ f becomes 9 -- Subtract one from a variable g= 3.7 g-- g becomes 2.7 += num Add a number to a variable current value h= 5 h += 2 h becomes 7 -= num Subtract a number from a variable current value i= 5 i -= 7 i becomes 3 *= num Multiply a variable by a number j= 5 j *= 2 j becomes 10 /= num Divide a variable by a number k= 5 k /= 2 k becomes 2.5 %= num A variable value becomes its remaineder from its division by a number l= 5 l %= 2 l becomes 1

The last point of this lesson is about operators orders. take a look at following statement and try to guess the result :

 Code Result Result= 2+3*5+18/6+(7-4)%2 alert (Result) ?

Parentheses are prior to other operators; the second place is for multiplication, dividing and percentaging operators ('*', '/', '%'); the one at left acts before the others from a category. The third place is for summing and subtracting ('+', '-'); and again the left operator is prior to the others.
So we can say that ((7-4= 3)) is calculated first, then ((3*5= 15)), after that ((18/6= 3)) and then ((3%2= 1)). Now it’s Summations and Subtractions that get ran ((2+15+3+1= 21)). The final result is 21.