JavaScript

How to Perform Mathematics in JavaScript Using Arithmetic Operators

Mathematical Operators play a very crucial role in JavaScript and also in other well-known programming languages. Numerous mathematical operations are available in JavaScript.  Mathematical operators are frequently used to calculate and process numerical data. In this post, we learn about the different arithmetic operators that are available in JavaScript to perform different mathematical operations. Such as addition, multiplication, division, modulus, etc. Arithmetic operators perform an operation and return a value.

First, we will understand the concept of operators and operands. The operators are special symbols that symbolize the computations such as addition, subtraction, etc. while the operands are the values upon which operators perform some actions. Consider a very basic example where we want to add two values:

Implementation of Addition “+” operator

var a=12 + 14;

console.log("The sum of two numbers : ", a);

In the above example, “+” is a mathematical operator while the numbers (“12” and “14”) are its operands.

In this example, we added two values and assign their result to a variable a. “console.log” is a method in JavaScript, that will print the output on the browser’s console.

The output of the above example will be:

In JavaScript, the values can be literals or variables, or expressions. In the above example, we have literals (12+ 14) while in the next example we will add the two numbers and assign the result to the third variable (a+ b):

let a= 12;

let b=14;

let c=a+ b;

console.log("The sum of a and b :" , c);

Implement the above code in JavaScript.

This example and the previous example will produce the same result:

Must remember that “+” will act as a concatenation operator when we add two strings. So, pay attention and use the “+” operator carefully.

Subtract “-” operator in JavaScript

The “-” operator is utilized to determine the difference of different numeric values. For instance, we consider an example to understand how the “-” operator works.

Implementation of Subtract “-” operator

var a= 12;

var b=14;

var c= a - b;

console.log(“the value of c :, c);

Assign a value 12 to variable a, assign 14 to var b, subtract the value of b from the value of a, and assign the result to c.

Here we use the console.log function to print the output on the console you can also use the console.log to display the output on the document:

Multiplication “*” operator in JavaScript

In JavaScript “*” is used to multiply the numerical values.

Implementation of Multiplication “*” operator

var a= 12;

var b=14;

var c= a * b;

console.log ("the product of a and b is : ", c);

c stores the result of a*b and “console.log” will display the value stored in c:

The output of the above-given program will be:

Division “/” operator in JavaScript

In JavaScript “/” is used for the division purpose. It is used to determine the quotient of the numeric values:

Implementation of Division “/” operator

var a= 12;

var b=14;

var c= a / b;

console.log("The result of a/b is : ", c);

Now implement it in the Visual Studio Code:

The output for the above-given code will be:

Modulus “%” operator in JavaScript

The modulus operator is accessible in JavaScript, it is depicted by the percentage sign (%) and it’s also known as the modulo operator, it is responsible to return the remainder value. In programming languages, the modulus operator is used to check wheatear the number is even or odd.

Implementation of Modulus “%” operator

var a= prompt("Enter a number");

if (a%2==0)

{

    console.log("you enter an even number");

}

else

{

    console.log("you enter an odd number");

}

In this example, a variable “a” will take a value from the user.

If a user enters an odd value it will print a message “you enter an odd value”. The modulus operator divides the given value with 2, if the remainder is 1 it will show an odd value:

If the user enters an even number;

Then it will print a message “you enter an even value”:

Exponentiation Operator

The exponentiation operator is one of the latest operators in JavaScript which is represented with a double asterisk (**). It is utilized to compute the power of a .

Implementation of Exponentiation “**” operator

var a= 3 ** 4;

console.log("3 power 4 is equal to : ", a);

Implement the above piece of code in JavaScript

The output of the above-given code will be:

In JavaScript, an alternate method is also available to find the power of a number.

Implementation of Math.pow Method

var a= Math.pow(3, 4);

console.log("calculate the power using pow function : ", a);

math.pow function is utilized in this example to calculate the power of a number.

The output of the math.pow method will be the same as the output of the exponentiation operator:

Increment operator (++) in JavaScript

The “++” operator increments the value of a variable one time. For instance, consider we have a variable whose value is 5, if we apply increment operator on it, then the increment operator will increment its value to 6. The increment operator can be applied only to the variable. We can’t apply the increment operator on numeric values it will result in an error. For example:

var a=5;

a++;      //correct, increments the value one time.

5++;     //false, it will cause an error.

Implementation of Increment operator

var a=5;

a++;

console.log("The incremented value is : ", a);

Implement the above code in JavaScript.

The output will be:

Decrement operator (–) in JavaScript

The “-” operator decrements the value of a variable one time. Let’s suppose we have a variable whose value is 5 the decrement operator will decrease it to 4. The decrement operator can be applied only to the variable. We can’t apply the decrement operator on numeric values it will result in an error. For example:

var a=5;

a--;      //correct, value will be decremented to 4.

5--;     //false, it will cause an error.

Implementation of decrement operator

var a=5;

a--;      //correct, value will be decremented to 4.

console.log ("The decremented value is : ", a);

The above-given code will be implemented in JavaScript as;

The output of the above-given code will be:

Operator Precedence in JavaScript

In JavaScript, the expression is evaluated on the basis of precedence (priority base). The programming languages follow the same precedence as we follow in mathematics. Parenthesis has the highest precedence, then exponents, then multiplication and division have the same precedence, addition and subtraction have the lowest precedence as compared to the other operators.

Implementation of Operator Precedence in JavaScript

Let’s consider an example to understand the precedence in JavaScript:

var a= 2 + 3 * 4 **2;

console.log("The result is : ", a);

Now we will implement the above code in visual studio code:

The output will verify that the above code follows the precedence order. According to this, it will first solve “4 **2” because exponents have higher precedence then it will multiply the result of exponents with the 3 and at the end, it will perform addition. So, the output is:

Conclusion:

Mathematical operators are very important to perform operations like addition, subtraction, and multiplication, etc. In JavaScript, while solving complex expressions, these arithmetic operations follow the same precedence as in regular mathematics.

In this article, initially, we determined what arithmetic operations are, their use in JavaScript, then we addressed various mathematical operators, their syntax in JavaScript and implemented them in visual studio code. Furthermore, we have learned about the operator precedence, and finally, we discussed a couple of arithmetic operators particularly for the programming languages such as increment and decrement operators.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.