JavaScript Arithmetic Operator


In this tutorial, you will learn about arithmetic operators in javascript with examples.

Arithmetic Operators

Arithmetic operators are the set of operators that performs mathematical operations on the operands like addition, subtraction, multiplication, division, etc.

Some of the arithmetic operators behave differently in different contexts. For example, the addition operator (+) adds 2 numbers but concatenate two strings.

There are the following arithmetic operators in javascript.

OperatorsSignDescriptionExample
Addition+Adds 2 numbers and concatenates 2 strings.

2 + 2 result: 4

'Hello' + ' World' result: 'Hello World'

Subtraction-Subtracts 2 numbers

2 - 2 result: 0

Multiplication*Multiplies 2 numbers

2 * 2 result: 4

Division/Divides 2 numbers

2 / 2 result: 1

Modulus%Returns the remainder of the division of 2 numbers

3 % 2 result: 1

Exponent**Returns the result of the first number raised to the power of the second number

2 ** 2 result: 4

Floor Division//Returns the floor of the division of 2 numbers

2 // 2 result: 1

Let's discuss each operator and see their examples.

1. Addition operator

Addition operator is used to add two operands. To add operands use + symbol. for example 10 + 15 or we can add variable var a = 5, b = 1, c; c = a + b;.

Example

var x = 10 + 15; // adding numbers
var y = 2.5 + 3.4; // float number
console.log(x, y);

var a = 5, b = 10, c;
c = a + b; // adding variables
var d = (1 + 2 + 3 + 4 + 5) * c; // addition in expression
console.log(d);
Try It

Concatenating String using + operator

The same operation sign + can concatenate strings also, which means it can attaching two strings side by side. For example, var greeting = "Hello," + " World!" output is Hello, World!.

Example

var a = "Hello," + " World";
var x = "tutorials";
var y = "tonight";
var z = ".com";
var b = x + y + z;
console.log(a, b);
Try It

Adding Strings and numbers

Javascript behaves strangely sometimes. It can add number string (like '2', '4.5', '-10', etc) with actual number (Data type - Number) and give string value. For example, var a = 5 + '5' is a valid expression and gives a string result.

Output of adding number string and numbers depends on orientation of operators. Javascript adds all numbers first and when it comes first string then it starts concatenating all coming operands. For example 2 + '2' = "22", 2 + 2 + '2' = "42", 2 + 2 + '2' + 1 = "421" and so on.

Example

var a = 2 + 2; // 4
var b = 2 + 2 + '2'; // 4 + '2' = "42"
var c = 10 + 5 + '5' + 5; // 15 + '5' + 5 => "155" + 5 => "1555"
console.log(a, b, c);
console.log(typeof a, typeof b, typeof c);
Try It

2. Subtraction operator

Subtraction operator is used to subtract two operands. To subtract operands use - symbol. for example 10 - 15 or we can subtract variable var a = 5, b = 1, c; c = a - b;.

Example

var x = 20 - 15;
var y = 9.5 + 3.4; // float number
var a = 5, b = 10, c;
c = a - b; // Subtracting variables
var d = (12 - 4 - 5) * c; // Subtracting in expression
console.log(x, y, d);
Try It

Subtracting numerical string

Unlike the addition operator which adds and concatenates both, the subtraction operator always subtracts.

Example

var a = 5 - 10; // -5
var b = 12 - 2 - '2'; // 10 - '2' = 8
console.log(a, b);
Try It

3. Multiplication operator

The multiplication operator is used to multiply two operands. To multiply operands use the * symbol. for example 10 * 15 or we can multiply variable var a = 5, b = 1, c; c = a * b;.

Example

var a = 5 * 2; // 10
var b = 6.1;
var c = 3.2;
var d = b * c; // 19.52
console.log(a, d);
Try It

4. Division operator

Division operator is used to divide two operands. To divide operands use / symbol. for example 10 / 15 or we can divide variable var a = 5, b = 1, c; c = a / b;.

Example

var a = 16 / 2; // 8
var b = 21;
var c = 7;
var d = b / c;
console.log(a, d);
Try It

5. Modulus operator

The modulus operator is used to find the remainder of the division. To find the remainder of the division uses the % symbol. for example 10 % 3 or we can find remainder of variable var a = 5, b = 1, c; c = a % b;.

Example

var a = 17 % 2; // 1
var b = 13;
var c = 7;
var d = b % c;
console.log(a, d);
Try It

6. Exponential operator

The exponential operator is used to raise one operand to the power of another. To raise operands use the ** symbol. for example 10 ** 2 or we can raise variable var a = 5, b = 1, c; c = a ** b;.

Javascript also has Math.pow(a,b) method to find exponentials.

Example

var a = 5 ** 2; // 5^2 = 25
var b = 3;
var c = 4;
var d = b ** c;
console.log(a, d);
Try It

7. Floor division operator

Floor division operator is used to find quotient of division. To find quotient of division use // symbol. for example 10 // 3 or we can find quotient of variable var a = 5, b = 1, c; c = a // b;.

Example

var a = 17 // 2; // 8
var b = 13;
var c = 7;
var d = b // c;
console.log(a, d);

Increment operator

Increment operator ++ is used to increase a number by 1.

Increment operator can be used in two ways:

Difference in pre-increment and post-increment : Both increase operand by 1 but pre-increment increase value before assigning value to the operand but post-increment increase value after assigning value to number. For example var a = 10; console.log(a++); // output 10 not 11 and var a = 10; console.log(++a); // output 11.

Example

var a = 10;
var b = 15;
var c = 12;
console.log(b + a++); // 15 + 10 = 25
console.log(a); // 11
// a = 11 now but first b + a was added then a was increased
console.log(b + ++a); // 15 + 12 = 27)
console.log(c++); // output: 12
Try It

Decrement operator

Decrement operator -- is used to decrease a number by 1.

Decrement operator can be used in two ways:

The difference in pre-decrement and post-decrement: Both decrease operand by 1 but pre-decrement decrease value before assigning value to the operand but post-decrement decrease value after assigning value to a number. For example var a = 10; console.log(a--); // output 10 not 9 and var a = 10; console.log(++a); // output 9.

Example

var a = 10;
var b = 15;
var c = 12;
console.log(c--); // output: 12
console.log(b + a--); // 15 + 10 = 25
console.log(a); // 9
// a = 9 now but first b + a was added then a was decreased
console.log(b + --a); // 15 + 8 = 23)
Try It