JavaScript Arithmetic Operators
Overview
JavaScript arithmetic operators are used to perform arithmetic operations on numbers. In this tutorial, we will learn how to use these operators with some examples.
JavaScript Arithmetic Operators
Some basic arithmetic operators available in JavaScript are as follows:
JavaScript Operator |
Description |
+ | The + operator is used to add two operands. |
– | The – operator is used for subtraction. The right-hand operand is subtracted from the left-hand operand. |
* | The * operator is used for multiplication. |
/ | The / operator is used for division. It divides the left operand by the right operand. |
% | The % is the modulus operator. It returns the remainder of the division of the left operand by the right operand. |
++ | The ++ operator is the increment operator. It adds 1 to its operand. |
— | The — operator is the decrement operator. It subtracts one from its operand. |
** | The ** operator is the exponentiation operator. The operator raises the left operand to the power of the right operand. |
Example
Learn how to perform arithmetic operations using JavaScript operators with examples.
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Arithmetic Operators</title>
<script type=”text/javascript”>
let x = 5, y = 10;
document.write(“JavaScript Arithmetic Operators”);
document.write(“<br>”);
document.write(“x=”+ (x));// print x
document.write(“<br>”);
document.write(“y=”+(y));// print y
document.write(“<br>”);
document.write(“—————————————–“);
document.write(“<br>”);
document.write(“Different Arithmetic Operations”);
document.write(“<br>”);
document.write(“x+y=”+(x+y));//return 15
document.write(“<br>”);
document.write(“y-x=”+(y-x));//returns 5
document.write(“<br>”);
document.write(“x*y=”+(x*y));//returns 50
document.write(“<br>”);
document.write(“y/x=”+(y/x)); //returns 2
document.write(“<br>”);
document.write(“x%2=” + (x%2));//returns 1
document.write(“<br>”);
document.write(“x++=”+(x++));//returns 5
document.write(“<br>”);
document.write(“x–=”+(x–)); //returns 6
document.write(“<br>”);
document.write(“x + x =”+(x + x)); //returns 10
document.write(“<br>”);
document.write(“JavaScript Tutorials – www.TestingDocs.com”);
document.write(“<br>”);
</script>
</head>
<body>
</body>
</html>
Output
JavaScript Arithmetic Operators
x=5
y=10
—————————————–
Different Arithmetic Operations
x+y=15
y-x=5
x*y=50
y/x=2
x%2=1
x++=5
x–=6
x + x =10
JavaScript Tutorials – www.TestingDocs.com
Experimenting with different operators and values is a great way to learn JavaScript.
—
JavaScript Tutorials
JavaScript tutorials on this website can be found at:
https://www.testingdocs.com/javascript-tutorial/
To learn more about JavaScript, visit