Arithmetic operators in JavaScript
What you will learn here about operators.
- Arithmetic operators in JavaScript
Arithmetic operators in JavaScript
JavaScript supports 7 Arithmetic operators and those are
- Addition ( + )
- Subtraction ( – )
- Multiplication ( * )
- Division( / )
- Modulo Division ( % )
- Increment ( ++ )
- Decrement ( — )
1)Addition ( + )
Addition operator used to add two number together.
var num1=100;
var num2=50
var num=num1+num2; //result is 150
Incase of String, Addition operator acts as concatenation operator.
var num=”Jon”+” Dow”; //result is “Jon Dow”
Number + String always gives String result but first addition is performed; Example is given below
var num=100+50+” Jon Dow”; //result is “150 Jon Dow”
String + number always gives String result. Example is given below.
var num=”Jon Dow “+100+50; //result is “Jon Dow 10050”
2)Subtraction ( – )
The Subtraction(-) operator is used subtract one number from another number.
var num2=50
var num=num1-num2; //result is 50
3)Multiplication ( * )
The Multiplication operator is used to multipy two numbers.
var num2=50
var num=num1*num2; //result is 5000
4)Division( / )
The Division operator is used to performe division between two numbers and returns the quotient of the division.
var num2=50
var num=num1/num2; //result is 2
5)Modulo Division ( % )
The Modulo Division operator is used to perform division between two numbers and returns the remainder of the division.
var num2=50
var num=num1%num2; //result is 0
6)Increment ( ++ )
Increment operator is used to increment number by 1. i++ is the post increment which first returns value then increment by value 1. ++i is the pre increment which first increment value by 1 and then returns the value.
7)Decrement ( — )
Decrement operator is used to decrement number by 1. i– is the post decrement which first returns value then decrement by value 1. –i is the pre increment which first decrement value by 1 and then returns the value.