Learn JavaScript

JavaScript Arithmetic

JavaScript arithmetic operators are used to perform some fundamental arithmetic operations. It includes the operators like: addition (+), subtraction (-), multiplication (*), exponentiation (**), division (/), modulus or remainder (%), increment (++), and decrement (–). Increment and decrement operators are also called the unary operators, since they operates on one operand only. Whereas the other arithmetic operators are the binary operators (require two operands to perform any manipulation).

In order to understand the working of an Arithmetic Operator let us assume, there are two variable a and b with initial values 2 and 6 respectively, then:

Arithmetic OperatorsDescription and Examples
Addition (+)It adds two operands.
Example: a + b produces the value 8.
Subtraction (-)It subtract the value of the second operand from the value of the first operand.
Example: a – b produces a value -4.
Multiplication (*)It multiply the values of both operands.
Example: a*b produces the value 12.
Division (/)It divides the numerator by denominator.
Example: b/a produces the value 3.
Modulus (%)It return the remainder in an integer division.
Example: b%a produces the value 0.
Increment (++)It increases the integer value of an operand by 1.
Example: a++ will give 3.
Decrement (–)It reduces the integer value of an operand by 1.
Example: a– will give 1.

Note: Addition arithmetic operator is used to add two numbers, while it is used to concatenate two strings.


Example on Arithmetic Operators

<html>
<body>
<h2> JavaScript Program / Code on Arithematic Operators</h2>
<script>
var a = 2, b = 6;
var add, sub, mul, div, mod, inc, dec;
add = a + b;
sub = a - b;
mul = a * b;
div = b/a;
mod = b%a;
inc = a++;
dec = b--;
document.write("Addition of " + a +' and ' + b +" is = " + add + "<br />");
document.write("Subtraction of " + a +' and ' + b +" is = " + sub + "<br />");
document.write("Multiplication of " + a +' and ' + b +" is = " + mul + "<br />");
document.write("Division of " + a +' and ' + b +" is = " + div + "<br />");
document.write("Modulus of " + a +' and ' + b +" is = " + mod + "<br />");
</script>
</body>
</html>

Output

JavaScript Program / Code on Arithmetic Operators

Addition of 2 and 6 is = 8
Subtraction of 2 and 6 is = -4
Multiplication of 2 and 6 is = 12
Division of 2 and 6 is = 3
Modulus of 2 and 6 is = 0


Example on JavaScript Increment Operator

<html>
<body>
<h2>JavaScript Program / Code on Increment Operator</h2>
<p id="jio"></p>
<script>
var a = 2;
a++;
var c = a;
document.getElementById("jio").innerHTML = c;
</script>
</body>
</html>

Output

JavaScript Program / Code on Increment Operator

3


Example on JavaScript Decrement Operator

<html>
<body>
<h2>JavaScript Program / Code on Decrement Operator</h2>
<p id="jdo"></p>
<script>
var b = 5;
b--;
var c = b;
document.getElementById("jdo").innerHTML = c;
</script>
</body>
</html>

Output

JavaScript Program / Code on Decrement Operator

4


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.