# JAVASCRIPT ARITHMETIC

Arithmetic operators in javascript are used for purposes like addition, subtraction, multiplication, division etc on the operators. There are many arithmetic operators in javascript, addition operator in javascript behave differently depending on operator type. We will see that in this chapter.

There are the following arithmetic operators in javascript.

sr no. | Operators | Description |
---|---|---|

1 | + | Addition |

2 | - | Subtraction |

3 | * | Multiplication |

4 | ** | Exponential (ES6) |

5 | / | Division |

6 | % | Modulus used to find remainder |

7 | ++ | Increment operation used to increase number by 1 |

8 | -- | Decrement operation used to decrease number by 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 in between them. for example `10 + 15`

or we can add variable
`var a = 5,b = 1, c; c = a + b;`

.

```
var n = 10 + 15; // adding numbers
var m = 2.5 + 3.4; // float number
var a = 5, b = 10, c;
c = a + b; // adding variables
var d = (1 + 2 + 3 + 4 + 5) * c; // addition in expression
```

▶ Run the code
### Concatenating String using + operator

The same operation sign `+`

is used to concatenate strings, means attaching two strings side by
side. For example `var greeting = "Hello," + " World!"`

output is **Hello,
World!** .

```
var a = "Hello," + " World";
var x = "tutorials";
var y = "tonight";
var z = ".com";
var b = x + y + z;
```

▶ Run the code
### Adding Strings and numbers

Javascript behaves strangely sometimes. It can add number string (Data type - String) with 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.

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

▶ Run the code
## 2. Subtraction operator

The subtraction operator is used to subtract two numbers. For example
`20 - 5`

also variables can be used `var a = 5,b = 1, c; c = a - b;`

.

```
var n = 20 - 15; // adding numbers
var m = 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
```

▶ Run the code
### Subtracting numerical string

Unlike addition operator which add and concatenate both, subtraction operator always subtract.

```
var a = 5 - 10; // -5
var b = 12 - 2 - '2'; // 10 - '2' = 8
```

▶ Run the code
## 3. Multiplication operator

Multiplication operator `*`

multiply operands.

```
var a = 5 * 2; // 10
var b = 6.1;
var c = 3.2;
var d = b * c; // 19.52
```

▶ Run the code
## 4. Exponential operator

Exponential operator `**`

find power of first operand to the second operator. Javascript also has
Math.pow(a,b) method to find exponentials.

```
var a = 5 ** 2; // 5^2 = 25
var b = 3;
var c = 4;
var d = b ** c;
```

▶ Run the code
## 5. Division operator

Division operator `/`

is used to divide two operands.

```
var a = 16 / 2; // 8
var b = 21;
var c = 7;
var d = b / c;
```

▶
Run the code
## 6. Modulus operator

Modulus operator `%`

is used to find the remainder two operands.

```
var a = 17 % 2; // 1
var b = 13;
var c = 7;
var d = b % c;
```

▶
Run the code
## 7. 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`

.

```
var a = 10;
var b = 15;
var c = 12;
console.log(c++); // output: 12
```

▶
Run the code
## 8. Decrement operator

Decrement operator `--`

is used to decrease a number by 1.

Decrement operator can be used in two ways :

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

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

**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 number. For example `var a = 10; console.log(a--); // output 10 not 9`

and
`var a = 10; console.log(++a); // output 9`

.

```
var a = 10;
var b = 15;
var c = 12;
console.log(c--); // output: 12
```

▶
Run the code
More Learning

Want to learn more about programming and web development? We have a few recommendations for you.