Learn JavaScript Arithmetic






In Javascript we use Arithmetic Operators to perform arithmetic on numbers (variables or literals).



Operator Description
* Multiplication
+ Addition
/ Division
- Subtraction
** Exponentiation (ES2016)
% Modulus (Division Remainder)
++ Increment
-- Decrement


Arithmetic Examples




Arithmetic using numbers (literals)

Code Example:

                    var a = 100 + 50;  
                 
                 
Try yourself >>





we can also use variables:

Code Example:

                     var a = b + c;
                 
                 
Try yourself >>





expressions:

Code Example:

                     var a = (10 + 5) * b;
                 
                 
Try yourself >>





Full list of examples:



The multiplication operator (*)


Code Example:

                   
                   var a = 10;
var b = 5;
var c = a * b;
                 
                 
Try yourself >>

The addition operator (+)


Code Example:

                   
                   var a = 40;
var b = 10;
var c = a + b;
                 
                 
Try yourself >>



The subtraction operator (-)


Code Example:

                   
                   var a = 40;
var b = 10;
var c = a - b;
                 
                 
Try yourself >>



The division operator (/)


Code Example:

                   
                   var a = 50;
var b = 10;
var c = a / b;
                 
                 
Try yourself >>



The increment operator (++)


Code Example:

                   
                   var a = 5;
a++;
var b = a;
                 
                 
Try yourself >>



The decrement operator (--)


Code Example:

                   
                   var a = 5;
a--;
var b = a;
                 
                 
Try yourself >>



The modulus operator (%)

returns the division remainder.


Code Example:

                   
                   var a = 5;
var b = 2;
var c = a % b;
//comment: with arithmetic, the division of two integers produces a quotient and a remainder.
//However with mathematics, the result of a modulo operation is the remainder of an arithmetic division.
                 
                 
Try yourself >>



The exponentiation operator (**)


Code Example:

                   
                   var a = 10;
var b = a ** 2; 
//comment:  raises the first operand to the power of the second operand.
// result will be 100.
                 
                 
Try yourself >>





Operator Precedence


Code Example:

                   
                   var a = 5 + 5 * 2;
                 
                 

Look at the example above, does the result of a = 5 + 5 * 2; the same as 10 * 2 ?, or as 5 + 10?, which operator being used by Javascript first?


As you learned at school, the multiplication is done first


Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).


And (as you have learned in school) the precedence can be changed by using parentheses as we show below:


Code Example:

                            
                            var a = (5 + 5) * 2;
                          
                          

While using parentheses, the operations inside the parentheses are computed first. While many operations have the same precedence (like addition and subtraction), they will be computed from left to right:











Learn to code, we offer free programming guides, our main goal is to allow students to learn web development easily from home.



Navigation