Arithmetic Operator
Arithmetic Operator
In JavaScript, arithmetic operators take numerical values as their operands and perform mathematical operations between them.
Some arithmetic operators are as follows addition (+), subtraction (-), multiplication (*), division (/) and more.
Operator |
Description |
Example |
---|---|---|
+ |
Addition |
10 + 5 = 15 |
- |
Subtraction |
10 – 5 = 5 |
* |
Multiplication |
10 * 5 = 50 |
/ |
Division |
10 / 5 = 2 |
% |
Modulus |
10 % 5 = 0 |
++ |
Increment |
var a = 10; a++; a = 11 |
-- |
Decrement |
var a = 10; a-- ; a = 9 |
Addition (+) Operator
The addition operator is mainly used to determine the sum of two numbers or operands.
Code:
<html>
<body>
<h2>The +(Addition) Operator</h2>
<script>
var x = 5 + 2;
document.write(x);
</script>
</body>
</html>
Here, the Addition (+) operator mainly adds the two operands which are 5 and 2. So, the output is 7.
Output:
Multiplication (*) Operator
The Multiplication Operator is mainly used to determine the product of two numbers.
Code:
<html>
<body>
<h2>The *(Multiplication) Operator</h2>
<script>
var x = 5 * 2;
document.write(x);
</script>
</body>
</html>
Here, the Multiplication (*) operator mainly multiplies the two operands which is 5 and 2. So, the output is 10.
Output:
Division (/) Operator
The division operator is mainly used to perform division operations.
Code:
<html>
<body>
<h2>The /(Division) Operator</h2>
<script>
var x = 10 / 2;
document.write(x);
</script>
</body>
</html>
Here, the Division (/) operator mainly divides the two operands which is 10 and 2. So, the output is 5.
Output:
Modulus (%) Operator
The modulus operator mainly returns the remainder of an integer division.
Code:
<html>
<body>
<h2>The %(Modulus) Operator</h2>
<script>
var x = 10 % 2;
document.write(x);
</script>
</body>
</html>
Here, the Modulus (%) operator mainly divides the two operands which is 10 and 2. So, the output is remainder of them is 0.
Output:
Increment (++) Operator
The increment operator is used to increments the numeric value of its operand by 1.
Code:
<html>
<body>
<h2>The ++(Increment) Operator</h2>
<script>
var x = 10;
x++;
document.write(x);
</script>
</body>
</html>
Here, the Increment (++) operator mainly increments the operand which is 10. So, the output is 11.
Output:
Decrement (--) Operator
The decrement operator mainly decrements the numeric value of its operand by 1.
Code:
<html>
<body>
<h2>The ++(Increment) Operator</h2>
<script>
var x = 10;
x--;
document.write(x);
</script>
</body>
</html>
Here, the Decrement (--) operator mainly decrements the operand which is 10. So, the output is 9.
Output: