The JavaScript Arithmetic Operators include operators like Addition, Subtraction, Multiplication, Division, and Modulus. All these operators are binary operators, which means they operate on two operands.
The below table shows the Arithmetic Operators in JavaScript with examples.
JS Arithmetic Operators | Operation | Example |
---|---|---|
+ | Addition | 10 + 2 = 12 |
– | Subtraction | 10 – 2 = 8 |
* | Multiplication | 10 * 2 = 20 |
/ | Division | 10 / 2 = 5 |
% | Modulus – It returns the remainder after the division | 10 % 2 = 0 (Here remainder is zero). If it is 10 % 3 then it will be 1. |
JavaScript Arithmetic Operators Example
For this JS Arithmetic operations example, We are using two variables a and b, and their values are 12 and 3.
We are going to use these two variables to perform various arithmetic operations in JavaScript Programming Language
<!DOCTYPE html> <html> <head> <title>Javascript Arithmetic Operators</title> </head> <body> <h1>Performing Arithmetic Operations </h1> <script> var a = 12, b = 3; var addition, subtraction, multiplication, division, modulus; addition = a + b; //addition of 3 and 12 subtraction = a - b; //subtract 3 from 12 multiplication = a * b; //Multiplying both division = a / b; //dividing 12 by 3 (number of times) modulus = a % b; //calculation the remainder document.write("Addition of " + a +' and ' + b +" is = " + addition + "<br />"); document.write("Subtraction of " + a +' and ' + b +" is = " + subtraction + "<br />"); document.write("Multiplication of " + a +' and ' + b +" is = " + multiplication + "<br />"); document.write("Division of " + a +' and ' + b +" is = " + division + "<br />"); document.write("Modulus of " + a +' and ' + b +" is = " + modulus + "<br />"); </script> </body> </html>
OUTPUT
JavaScript Arithmetic Operations Example 2
For this JS example also, We are using two variables a and b and their values are 12 and 3.
Here. we are going to use these variables to show you, How to display the JavaScript output in Paragraphs.
<!DOCTYPE html> <html> <head> <title>Javascript Arithmetic Operators 2</title> </head> <body> <h1>Performing Arithmetic Operations </h1> <p id = 'add'> Addition </p> <p id = 'sub'> Subtraction </p> <p id = 'mul'> Multiplication </p> <p id = 'div'> Division </p> <p id = 'mod'> Modulus </p> <script> var a = 12, b = 3; var add, sub, mul, div, mod; add = a + b; //addition of 3 and 12 sub = a - b; //subtract 3 from 12 mul = a * b; //Multiplying both div = a / b; //dividing 12 by 3 (number of times) mod = a % b; //calculation the remainder document.getElementById("add").innerHTML = "Addition of " + a +' and ' + b +" is = " + add; document.getElementById("sub").innerHTML = "Subtraction of " + a +' and ' + b +" is = " + sub; document.getElementById("mul").innerHTML = "Multiplication of " + a +' and ' + b +" is = "+ mul; document.getElementById("div").innerHTML = "Division of " + a +' and ' + b +" is = " + div; document.getElementById("mod").innerHTML = "Modulus of " + a +' and ' + b +" is = " + mod; </script> </body> </html>
OUTPUT