Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

JavaScript | Arithmetic Operators

  • Last Updated : 20 Jun, 2021

JavaScript Arithmetic Operators are the operators that operate upon the numerical values and return a numerical value. There are many operators in JavaScript. Each operator is described below along with its example.

1. Addition (+) The addition operator takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

a + b

Example:



// Number + Number => Addition 
1 + 2 gives 3

// Number + String => Concatenation
5 + "hello" gives "5Hello"

2. Subtraction (-) The subtraction operator gives the difference of two operands in the form of numerical value.

Syntax:

a - b

Example:

// Number - Number => Subtraction 
10 - 7 gives 3

"Hello" - 1 gives Nan

3. Multiplication (*) The multiplication operator gives the product of operands where one operand is multiplicand and another is multiplier.

Syntax:

a * b

Example:

// Number * Number => Multiplication
3 * 3 gives 9
-4 * 4 gives -16

Infinity * 0 gives NaN
Infinity * Infinity gives Infinity
'hi' * 2 gives NaN

4. Division (/) The division operator provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.

Syntax: 



a / b

Example:

// Number / Number => Division
5 / 2 gives 2.5
1.0 / 2.0 gives 0.5

3.0 / 0 gives Infinity
4.0 / 0.0 gives Infinity, because 0.0 == 0
2.0 / -0.0 gives -Infinity

5. Modulus (%) The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as remainder operator. It takes the sign of the dividend.

Syntax:

a % b

Example:

// Number % Number => Modulus of the number

9 % 5 gives 4
-12 % 5 gives -2
1 % -2 gives 1
5.5 % 2 gives 1.5
-4 % 2 gives -0

NaN % 2 gives NaN

6. Exponentiation (**) The exponentiation operator gives the result of raising the first operand to the power of the second operand. The exponentiation operator is right-associative. 

Syntax:

a ** b

In JavaScript, it is not possible to write an ambiguous exponentiation expression i.e. you cannot put an unary operator (+ / – / ~ / ! / delete / void) immediately before the base number.

Example:

// Number ** Number => Exponential of the number

-4 ** 2 // This is an incorrect expression
-(4 ** 2) gives -16, this is a correct expression
2 ** 5 gives 32
3 ** 3 gives 27
3 ** 2.5 gives 15.588457268119896
10 ** -2 gives 0.01
2 ** 3 ** 2 gives 512
 
NaN ** 2 gives NaN

7. Increment (++) The increment operator increments (adds one to) its operand and returns a value.

  • If used postfix  with operator after operand (for example, x++), then it increments and returns the value before incrementing.
  • If used prefix with operator before operand (for example, ++x), then it increments and returns the value after incrementing.

Syntax: 



a++ or ++a

Example:

// Postfix 
var a = 2;
b = a++; // b = 2, a = 3

// Prefix
var x = 5;
y = ++x; // x = 6, y = 6

8. Decrement (–) The decrement operator decrements (subtracts one from) its operand and returns a value.

  • If used postfix, with operator after operand (for example, x–), then it decrements and returns the value before decrementing.
  • If used prefix, with operator before operand (for example, –x), then it decrements and returns the value after decrementing.

Syntax:

a-- or --a

Example:

// Prefix
var x = 2;
y = --x; gives x = 1, y = 1

// Postfix 
var x = 3;
y = x--; gives y = 3, x = 2

9. Unary (-) This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.

Syntax:

-a

Example:

var a = 3;
b = -a; gives b = -3, a = 3

// Unary negation operator
// can convert non-numbers
// into a number
var a = "3";
b = -a; gives b = -3

10. Unary (+) This is a way to convert a non-number into a number. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.

Syntax:

+a

Example:

+4     gives 4
+'2'   gives 2
+true  gives 1
+false gives 0
+null  gives 0
My Personal Notes arrow_drop_up
Recommended Articles
Page :