There are many operations that are executed in a JavaScript program. These operations require proper representation in the program for the compiler to identify them and to carry out the scripted operation.
For example, if you have a basic arithmetic equation 4+3 and want to solve it using JavaScript, how will you make the JavaScript program understand and solve the equation? For this, there are many types of predefined operators in JavaScript. The 4 and 3 in the equation are identified as operands, and the ‘+’ is identified as an operator by JavaScript.
Following are the types of operators in JavaScript:
- Arithmetic Operators for various arithmetic equations
- Comparison Operators for comparing various data values
- Logical (Relational) Operators for various logical operations
- Bitwise Operators
- Assignment Operators for assignment requirements
- Conditional (ternary) Operators for evaluation purposes
Understanding Arithmetic Operators
Following are the arithmetic operators that JavaScript supports:
For example: Let variable X be 4, and variable Y be 2
Addition Operation:
Represented by +
The addition operator is used to add two operands
For example: X + Y output will be 6
Subtraction Operation
Represented by-
The subtraction operator is used to subtract the second operand from the first operand.
For example: X - Y output will be 2
Multiplication Operation
Represented by *
The multiplication operator is used to multiply the two operands
For example: X * Y output will be 8
Division Operation
Represented by /
The division operand is used to divide the numerator by the denominator
For example: X / Y output will be 2
Modulus Operation
Represented by %
The modulus operator is used for the remainder of an integer division
For example: X % Y output will be 0
Increment Operation
Represented by ++
The increment operator is used to increase the value of the input by one
For example: X++ output will be 5
Decrement operation
Represented by --
The decrement operator is used to decrease the value of the input by one
For example: X-- output will be 3
Following is an example of a JavaScript code using above mentioned arithmetic operators:
<html>
<body>
<script type = "text/javascript">
<!--
var x = 4;
var y = 2;
var z = "Execute";
var newline = "<br />";
document.write("x + y = ");
output = x + y;
document.write(output);
document.write(newline);
document.write("x - y = ");
output = x - y;
document.write(output);
document.write(newline);
document.write("x / y = ");
result = x / y;
document.write(output);
document.write(newline);
document.write("x % y = ");
result = x % y;
document.write(output);
document.write(newline);
document.write("x + y + z = ");
result = x + y + z;
document.write(output);
document.write(newline);
x = ++x;
document.write("++x = ");
result = ++x;
document.write(output);
document.write(newline);
x= --x;
document.write("--x = ");
result = --x;
document.write(output);
document.write(newline);
//-->
</script>
Set the variables to different values and then try...
</body>
</html>
The output for the program is
x + y = 6
x - y = 2
x / y = 2
x % y = 0
x + y + z = 6Execute
++x = 5
--x = 3