 Hello everyone, I hope you are fine. In today’s tutorial, we are gonna take a look at “Mathematical operators in JavaScript” in detail. Performing math operator in different programing language is very common. JavaScript also allows several operators to help us in numbers.

The mathematical operator in JavaScript is used for performing certain different tasks such as the final price of a financial transaction, browser windows size and calculating the range between the element in a website document.

There are two types of operator that I am going to discuss in this tutorial.

1. Arithmetic operator
2. Assignment operator

## Arithmetic Operators in JavaScript

To perform an action between two variable or values we use mathematical operators. In the equation 3+2 = 5 (+ is a syntax).

Java script has many different operators some are basic math operator and others are additionals operators particular to programming.

A table of JavaScript Operators

 Operator Description Example Definition + Addition 2 + 3 Sum of 2 and 3 – Subtraction 9 – 2 Difference of 9 and 2 * Multiplication 3 * 2 Product of 3 and 2 / Division 10 / 5 Division of 10 and 5 % Modulo 9 % 2 Remainder of 9 and 2 ** Exponentiation 9 ** 3 9 to the 3 power ++ Increment 9++ 9 plus one — Decrement 9– 9 minus one

Let me explain to you all of these operators in detail.

### Addition and Subtraction in JavaScript

Addition and subtractions are the operators from basic maths. They are used to find the sum and difference of two numerical values.  JavsScript has its own built-in calculator. All these mathematical operators can be directly performed in the console.

As you have seen a simple example of addition and now I am going to explain it with detail. Now I will assign numerical values to a and b and place the sum in c

• Suppose: a = 3;   b = 4;
• Now add a + b and assign the sum of a+b to c
• c = a + b;  alert(c);

Output

In a similar way, subtraction(-) also work with the same formula but will subtract the number.

• Suppose: a = 6;  b = 3;
• Now subtract a – b and assign the diffrence of a-b to c
• c = a – b;  alert(c);

Output We can also do add and subtraction by the float (decimals) and negative numbers. Look below with an example.

• a = -6.3;
• Now subtract a – b and assign the difference of a-b to c
• c = a – b;  alert(c);

Output There is one another fact about the (+)operator that you should know that adding a numeric number and string.  As we all know that 1 + 2 return the value of 3. But this will have an unexpected result.

Output: The result of this will be 12 because of the one string value(“2”),  javascript concatenate (Concat() function is used to join two or more string together)  it rather than adding 1 and 2. So it is really important to be careful because while coding in JavaScript. Therefore JavaScript is case sensitive language.

### Multiplication in JavaScript

Multiplication (*) and division (/) are also used in Javascript to find the numeric values by division or multiplication.  Multiplication could be used many times like calculating the price of any item etc and division help you in finding the time like the number of hours or minutes.

Output ### Divison Operator in JavaScript

Output ### Modulus in JavaScript

Modulo (%) is used to find the remainder of a quotient after division. It can help us find in that even if the number is even or odd. It is less familiar operator than others. It is also known as Modulus too.

Output ### Exponentiation in JavaScript

Exponentiation (**) is the new operator in JavaScript. Its calculate the power of a number by its exponent. Its written like this 8^4 (Eight to the 4 power).

Output 8 ** 4 represents the same as 8 multiplied by 8 four times:

8 * 8 * 8 * 8

### Increment and Decrement in JavaScript

The increment operator (++) increase the numerical value of a variable by one and on the other side decrement operator (–) decrease the numerical value of a variable by one. They are often used with loops.

You can use these operators with variable only and using them with raw numbers can only result in an error. Look in the example. Increment and Decrement are further divided into two classes: prefix operation and postfix operation. Firstly,  prefix operator.

prefix = ++x;

Output ### Postfix Operator in JavaScript

postfix = x++;

Output

The value of x is not increased in this postfix operator, The value of the x will not be increased until after the expression has been evaluated. But running the operation twice will increase the value.

All these increment and decrement operators are used in for loops most of the time.

## Assignment Operators in JavaScript

The assignment operator (=) is the most used operator, which we have used already. We used the assignment operator to assign the value to the variable.

//Assign 50\$ to price variable

Price = 50\$

JavaScript also contains compound assignment operators. Let’s discuss a compound assignment operator example.

//Assign 50\$ to price variable

Price = 50\$

Price += 5\$;

The output of this result will be 55\$. In this case, price += 5\$ is same as price = price + 5\$.

The arithmetic operator can be combined with the assignment operator to make a compound assignment operator. Down below is a table of the assignment operator.

 Description Operator Assignment = Addition assignment += Subtraction assignment -= Multiplication assignment *= Division assignment /= Exponentiation assignment **= Remainder assignment %=

In this article, we have discussed the arithmetic operator and their syntax in detail. If you have a question regarding this post you can ask me. I will reply to all of your queries.

Lastly, thank you for reading this article, and we are working hard to deliver you best content.