The basic mathematical operators in JavaScript include the following −
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
Let’s see how Arithmetic operators work and what does it include −
Sr.No | Operator and Description |
1 | + (Addition) Adds two operands Ex: A + B will give 30 |
2 | - (Subtraction) Subtracts the second operand from the first Ex: A - B will give -10 |
3 | * (Multiplication) Multiply both operands Ex: A * B will give 200 |
4 | / (Division) Divide the numerator by the denominator Ex: B / A will give 2 |
5 | % (Modulus) Outputs the remainder of an integer division Ex: B % A will give 0 |
6 | ++ (Increment) Increases an integer value by one Ex: A++ will give 11 |
7 | -- (Decrement) Decreases an integer value by one Ex: A-- will give 9 |
Example
You can try to run the following code to learn how to work Arithmetic Operators in JavaScript −
<html> <body> <script> var a = 77; var b = 30; var c = "Demo"; var linebreak = "<br />"; document.write("a + b = "); result = a + b; document.write(result); document.write(linebreak); document.write("a - b = "); result = a - b; document.write(result); document.write(linebreak); document.write("a / b = "); result = a / b; document.write(result); document.write(linebreak); document.write("a % b = "); result = a % b; document.write(result); document.write(linebreak); document.write("a + b + c = "); result = a + b + c; document.write(result); document.write(linebreak); a = ++a; document.write("++a = "); result = ++a; document.write(result); document.write(linebreak); b = --b; document.write("--b = "); result = --b; document.write(result); document.write(linebreak); </script> </body> </html>