Arithmetic Operations in Javascript


Introduction

Whenever we perform any arithmetic operation , there are 2 quantities involved i.e the operator and the operand for e.g. 2 + 6 . In this case 2 and 6 are operands however + is an operator. Javascript supports the following arithmetic operators. + , - , * , / , % , ++ , -- . Let's understand each one of them with suitable examples.


+ [Addition]

This is used to add two or more numbers.We can also used this operator for concatenation.


		        
// Adding 2 numbers Without declaring the variables
2 + 3   
//Output : 5

// Adding 2 number With one variable used for storing result.

var c = 10.05 + 20.09
console.log(c); // It is used to print the output on console.
//Output : 30.95

//Variables are used.
var a = 10
var b = 20
var c = a + b
console.log(“The value of c is ” + c)
//Output : The value of c is 30

//Performing concatination on a var and string.
var a = 10
var b = 'a'
var c = a + b
console.log(“The value of c is ” + c)
//Output : The value of c is 10a

//Performing concatination on 2 strings
var a = 'nodejsera'
var b = 'javascript'
var c = a + b
console.log(“The value of c is ” + c)
//Output : The value of c is 10a

                
	            





- [Subtraction]

This is used to Subtract the 2nd operand from the 1st one.


		        
// Subtracting 2 numbers Without declaring the variables
2 - 3   
//Output : -1

// Subtracting 2 number With one variable used for storing result.

var c = 20.09 - 10.05 
console.log(c); 
//Output : 10.04

//Variables are used.
var a = 10
var b = 20
var c = b - a 
console.log(“The value of c is ” + c)
//Output : The value of c is 10

                
	            





* [Multiplication]

This is used to Multiply the two operands.


		        
// Multiplying 2 numbers Without declaring the variables
2 * 3   
//Output : 6

// Multiplying 2 number With one variable used for storing result.

var c = 100 * 2.5
console.log(c); 
//Output : 250

//Variables are used.
var a = 100
var b = 20
var c = a * b
console.log(“The value of c is ” + c)
//Output : The value of c is 2000

                
	            





/ [Division]

This is used to divide the numerator from the denominator.


		        
// Dividing 2 numbers
18/6 
//Output : 6

// Dividing 2 number With one variable used for storing result.

var c = 20/5
console.log(c); 
//Output : 4

//Variables are used.
var a = 10
var b = 20
var c = b / a 
console.log(“The value of c is ” + c)
//Output : The value of c is 2

                
	            





% [Modulus]

This is used to get the remainder after diving the numerator with the denominator.


		        
// Modulus operator used on 2 numbers Without declaring the variables
2 % 3   
//Output : 2

//Modulus operator used on 2 number With one variable used for storing result.

var c = 100%5
console.log(c); 
//Output : 0

//Variables are used.
var a = 6
var b = 20
var c = b % a 
console.log(“The value of c is ” + c)
//Output : The value of c is 2

                
	            





++ [Increment]

This is used to increment the value by 1.


		        
// Increment operator used to increase the value by 1.
var a = 10;
a++ 
//Output :11



                
	            





-- [Decrement]

This is used to Decrement the value by 1.


		        
// Decrement operator used to decrement the value by 1.
var a = 10;
a-- 
//Output :9