Updated March 23, 2023
Introduction to Arithmetic Operators in JavaScript
In the era of web development, javascript is one of the most popular and commonly used programming languages used by programmers. It is a client-side language and has a very large library with many inbuilt functions created for the ease of programming. Like other programming languages, Javascript also allows programmers to perform various operations on numbers. There are many types of Operators used in Javascript like Arithmetic Operators, Bitwise Operator, Assignment Operators, Comparison Operators, etc. Arithmetic Operators take numerical values either variables or literals and perform the operation on its operands to produce the final result. Generally, arithmetic operators work in the same way in Javascript like other programming languages except for the division operation.
Types of Arithmetic Operators with Examples
The most commonly used operators in any programming language are the Arithmetic ones. There are various Arithmetic Operators used in Javascript in order to perform the operations between the two or more numerical values or the variables holding those numerical values. Arithmetic Operators used in Javascript along with their examples are given below:
1. Addition
An addition operator produces the sum of two or more numerical values or the variables holding those numerical values. In the case of strings, the addition operator does the String concatenation.
Example #1
This is the example of the addition of 2 numerical values.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Addition of numerical values</p>
<p id="p1"></p>
<script>
var sum = 70 + 30;
document.getElementById("p1").innerHTML = sum;
</script>
</body>
</html>
Output:
In the above example, no variables are used to perform the addition between the 2 numbers instead addition of these 2 numbers are assigned directly to a variable named ‘sum’ and the value returned by the ‘sum’ is printed in the paragraph.
Example #2
This is the example of the addition of 2 string values.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Addition of String values</p>
<p id="p1"></p>
<script>
var name = "rakesh" + " " + "yadav";
document.getElementById("p1").innerHTML = name;
</script>
</body>
</html>
Output:
In the above example, ‘rakesh’ and ‘yadav’ are the 2 strings and the variable ‘name’ is concatenating these 2 strings using the simple ‘+’ sign. So printing the value of variable ‘name’ in the paragraph ‘p’ is returning the full name as ‘rakesh yadav’.
2. Subtraction
As it is genuine that subtraction operator is used for the subtraction of 2 numbers or the variables holding those numbers. The operator used is ( – ).
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Subtraction of numerical values</p>
<p id="p1"></p>
<script>
var diff = 70 - 30;
document.getElementById("p1").innerHTML = diff;
</script>
</body>
</html>
Just like in addition, no extra variables are used to hold the values. Instead, subtraction expression is written and its value is assigned to a variable named ‘diff’. The final result of the difference is to get printed in the paragraph using the variable ‘diff’.
Output:
3. Multiplication
Multiplication operator is used for the multiplication of values (either numbers or the variables holding those values).
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Multiplication of numerical values</p>
<p id="p1"></p>
<script>
var num1 = 7;
var num2 = 3;
var mult = num1 * num2;
document.getElementById("p1").innerHTML = mult;
</script>
</body>
</html>
Output:
In the above example, ‘num1’ and ‘num2’ are the 2 variables used in order to hold the values and the multiplication is performed between them using the (*) operator and its result gets assigned to a variable ‘mult’. The value of the variable ‘mult’ is then displayed in the HTML paragraph.
4. Division
Division operator is used for the division of the 2 numbers where the left operand is dividends and the right operand is the divisor. Operator used for division is ( / ). Unlike other programming languages like C, Java, etc. Javascript produces the floating value as output instead of an integer.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Division of numerical values</p>
<p id="p1"></p>
<script>
var div = 70 / 30;
document.getElementById("p1").innerHTML = div;
</script>
</body>
</html>
Output:
In the above example, the ‘div’ variable is used and the result of 2 numerical values produced using the (/) operator is assigned to it. The result of ‘div’ is then displayed in the HTML paragraph.
5. Modulus
The modulus operator returns the remainder returned on dividing the 2 numbers.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Modulus returned on division of numerical values</p>
<p id="p1"></p>
<script>
var num1 = 70;
var num2 = 30;
var mod = 70 % 30;
document.getElementById("p1").innerHTML = mod;
</script>
</body>
</html>
Output:
In the above example, modulus or remainder is returned using the (%) operator and the results are assigned to a variable ‘mod’ whose value gets displayed in the paragraph.
6. Increment
There are some operators in Javascript which work on a single operand, which means instead of 2 operands which are required in operations like addition, subtraction, multiplication, division, etc. Instead, unard operator uses only a single operand or variable to perform an operation. The increment is a unary operator used in Javascript and increments the value of the variable by 1. ++ sign is added after the name of the variable in order to increment its value by 1.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Increment of a variable </p>
<p id="p1"></p>
<script>
var number = 70;
number++;
document.getElementById("p1").innerHTML = number;
</script>
</body>
</html>
Output:
In the above example, the value of the variable named ‘number’ is incremented by using the (++) sign after the variable name. The incremented value of the above variable is then displayed in the HTML paragraph.
7. Decrement
This operator is just the opposite of the Increment operator. It is also a unary operator and takes only 1 variable holding the value to perform its operation. It decrements the value of the variable by 1.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This is an example of Decrement of a numerical value</p>
<p id="p1"></p>
<script>
var number = 70;
number --;
document.getElementById("p1").innerHTML = number;
</script>
</body>
</html>
Output:
In the above example, the value of the variable named ‘number’ is decremented by using the (–) sign after the variable name. The decremented value of the above variable is then displayed in the HTML paragraph.
Conclusion
The above explanation clearly describes the arithmetic operators used on javascript. There are various types of operators and each type of operator has its own purpose. These operators are very basic concepts in any programming language and are taught in the starting only to the newbies before digging into the deep concepts. So it is very important to understand their use and purpose thoroughly as they form the basis of understanding for a programmer.
Recommended Articles
This is a guide to Arithmetic Operators in JavaScript. Here we discuss the types of Arithmetic Operators in JavaScript along with examples and code implementation. You can also go through our suggested articles to learn more –