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.

Operators Sign Description Example

`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.

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;`.

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!.

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.

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;`.

Subtracting numerical string

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

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;`.

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;`.

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;`.

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.

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;`.

Increment operator

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

Increment operator can be used in two ways:

• pre-increment: When Increment operator is used before the operand. Example - `++a;`
• post-increment: When Increment operator is used after the operand. Example - `a++;`

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`.

Decrement operator

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

Decrement operator can be used in two ways:

• pre-decrement : When the decrement operator is used before the operand. Example - `--a;`
• post-decrement : When decrement operator is used after the operand. Example - `a--;`

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`.