# JavaScript Arithmetic Operators

## How to use the addition operator (+)

The addition operator + is used to add two or more values together in JavaScript. The basic syntax for using the addition operator is as follows:

``value1 + value2;``

For example:

``````let x = 5;
let y = 10;
let sum = x + y;
console.log(sum); // 15``````

You can also use the addition operator to add multiple values together:

``````let x = 5;
let y = 10;
let z = 15;
let sum = x + y + z;
console.log(sum); // 30``````

You can also use the addition operator to add a value to a variable and reassign the result to the same variable:

``````let x = 5;
x += 3;
console.log(x); // 8``````

This is equivalent to writing x = x + 3;.

It is also possible to use the addition operator to concatenate strings, where the operator combines two or more strings together:

``````let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // "John Doe"``````

It is important to note that when using the addition operator + with numbers, JavaScript will perform mathematical addition, but when used with strings, it will perform string concatenation.

## How to use the multiplication operator (*)

The multiplication operator * is used to multiply two or more values together in JavaScript. The basic syntax for using the multiplication operator is as follows:

``value1 * value2;``

For example:

``````let x = 5;
let y = 10;
let product = x * y;
console.log(product); // 50``````

You can also use the multiplication operator to multiply multiple values together:

``````let x = 5;
let y = 10;
let z = 15;
let product = x * y * z;
console.log(product); // 750``````

You can also use the multiplication operator to multiply a value with a variable and reassign the result to the same variable:

``````let x = 5;
x *= 3;
console.log(x); // 15``````

This is equivalent to writing x = x * 3;.

It is important to note that when using the multiplication operator (*), JavaScript will perform mathematical multiplication on the values and return the result.

The operator expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.

## How to use the exponentiation operator (**) in JavaScript

In JavaScript, the exponentiation operator ** is used to raise a number to a specific power. It was introduced in ECMAScript 2016, before that you could use Math.pow() function.

The basic syntax for using the exponentiation operator is as follows:

``base ** exponent;``

Here, the base is the number you want to raise to a power, and the exponent is the power to which you want to raise the base.

For example:

``````let x = 2;
let y = 3;
let result = x ** y;
console.log(result); // 8``````

This example raises 2 to the power of 3, which is equal to 8 (2 * 2 * 2).

You can also use the operator to reassign a new value to a variable:

``````let x = 2;
x = x ** 3;
console.log(x); // 8``````

This is equivalent to writing x = 2 ** 3;.

It is important to note that the exponentiation operator ** expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.

Also, you can chain the operator with other mathematical operator like:

``````let x = 2;
let y = 3;
let z = 4;
let result = (x + y) ** z;
console.log(result); // 4096``````

Here x + y is evaluated first to 5 and then 5 is raised to the power of 4.

The exponentiation operator is right-associative, meaning that expressions are evaluated from right to left.

## How to use the division operator (/) in JavaScript

In JavaScript, the division operator / is used to divide one value by another. The basic syntax for using the division operator is as follows:

``dividend / divisor;``

Here, the dividend is the value that is being divided and the divisor is the value that the dividend is being divided by.

For example:

``````let x = 10;
let y = 2;
let quotient = x / y;
console.log(quotient); // 5``````

This example divides 10 / 2, which is equal to 5.

You can also use the division operator to divide a variable by a value and reassign the result to the same variable:

``````let x = 10;
x /= 2;
console.log(x); // 5``````

This is equivalent to writing x = x / 2;.

It is important to note that the division operator / expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.

Also, when dividing by zero, JavaScript returns infinity or -infinity, and when dividing a non-finite number by infinity, JavaScript returns zero.

Also, you can chain the operator with other mathematical operator like:

``````let x = 10;
let y = 2;
let z = 4;
let result = (x * y) / z;
console.log(result); // 5``````

Here x * y is evaluated first to 20 and then 20 is divided by 4.

It is important to note that the division operator is left-associative, meaning that expressions are evaluated from left to right.

## How to use the Modulus operator (%) in JavaScript

In JavaScript, the modulus operator % is used to find the remainder of a division operation. The basic syntax for using the modulus operator is as follows:

``dividend % divisor;``

Here, the dividend is the value that is being divided and the divisor is the value by which the dividend is being divided. The result is the remainder of the division.

For example:

``````let x = 10;
let y = 3;
let remainder = x % y;
console.log(remainder); // 1``````

This example divides 10 / 3, the quotient is 3 and the remainder is 1.

You can also use the modulus operator to find the remainder of a variable and reassign the result to the same variable:

``````let x = 10;
x %= 3;
console.log(x); // 1``````

This is equivalent to writing x = x % 3;.

It is important to note that the modulus operator % expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation. Also, when the divisor is zero, JavaScript returns NaN.

Also, you can chain the operator with other mathematical operator like:

``````let x = 10;
let y = 3;
let z = 4;
let result = (x * y) % z;
console.log(result); // 2``````

Here x * y is evaluated first to 30 and then 30 is divided by 4 and the remainder is 2.

It is important to note that the modulus operator is left-associative, meaning that expressions are evaluated from left to right.

## How to use the Increment operator (++) in JavaScript

In JavaScript, the increment operator ++ is used to increase a variable's value by 1. There are two forms of the increment operator: the pre-increment operator ++x and the post-increment operator x++.

The pre-increment operator ++x increments the variable's value by 1 and then returns the new value. The basic syntax for using the pre-increment operator is as follows:

``++x;``

For example:

``````let x = 5;
console.log(++x); // 6
console.log(x); // 6``````

The post-increment operator x++ increments the variable's value by 1 but returns the original value before the increment. The basic syntax for using the post-increment operator is as follows:

``x++;``

For example:

``````let x = 5;
console.log(x++); // 5
console.log(x); // 6``````

You can also use the increment operator to increment a value and reassign the result to the same variable:

``````let x = 5;
x = x + 1;
console.log(x); // 6``````

This is equivalent to writing x++;.

It is important to note that the increment operator expects an operand that is a number, if not it will try to convert it to a number before performing the operation.

Also, you can chain the operator with other mathematical operator like:

``````let x = 5;
let y = 3;
let result = (x + y) * (++x);
console.log(result); // 36``````

Here x is incremented before multiplying so x = 6 and x + y is evaluated to 6 + 3 which is 9 and then 9 * 6 which is 54.

It is important to note that the increment operator is right-associative, meaning that expressions are evaluated from right to left.

## How to use the Decrement operator (--) in JavaScript

In JavaScript, the decrement operator -- is used to decrease a variable's value by 1. There are two forms of the decrement operator: the pre-decrement operator --x and the post-decrement operator x--.

The pre-decrement operator --x decrements the variable's value by 1 and then returns the new value. The basic syntax for using the pre-decrement operator is as follows:

``--x;``

For example:

``````let x = 5;
console.log(--x); // 4
console.log(x); // 4``````

The post-decrement operator x-- decrements the variable's value by 1 but returns the original value before the decrement. The basic syntax for using the post-decrement operator is as follows:

``x--;``

For example:

``````let x = 5;
console.log(x--); // 5
console.log(x); // 4``````

You can also use the decrement operator to decrement a value and reassign the result to the same variable:

``````let x = 5;
x = x - 1;
console.log(x); // 4``````

This is equivalent to writing x--;.

It is important to note that the decrement operator -- expects an operand that is a number, if not it will try to convert it to a number before performing the operation.

Also, you can chain the operator with other mathematical operator like:

``````let x = 5;
let y = 3;
let result = (x + y) * (--x);
console.log(result); // 36``````

Here x is decremented before multiplying so x = 4 and x + y is evaluated to 4 + 3 which is 7 and then 7 * 4 which is 28.

It is important to note that the decrement operator is right-associative, meaning that expressions are evaluated from right to left.