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

.

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

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

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

.

### Subtracting numerical string

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

## 3. Multiplication operator

Multiplication operator `*`

multiply operands.

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

## 5. Division operator

Division operator `/`

is used to divide two operands.

## 6. Modulus operator

Modulus operator `%`

is used to find the remainder two operands.

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

.

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

.