JavaScript supports different types of operators such as Comparison operators, Bit-wise operators, Operator, Arithmetic operators, and Assignment operators. In between other operators, Arithmetic operators are utilized for performing mathematical operations on operands such as Subtraction, Addition, Division, Multiplication, Increment, and Decrement.
This write-up will discuss the usage of Arithmetic operators in JavaScript. So let’s start!
Arithmetic operators in JavaScript
Here is the list of Arithmetic operators supported by JavaScript:
- Addition operator (+)
- Subtraction operator (-)
- Multiplication operator (*)
- Division operator (/)
- Modulus operator (%)
- Exponentiation operator (**)
- Increment operator (++)
- Decrement operator (–)
We will now discuss the usage of each of the mentioned Arithmetic operators in the following sections.
How to use Addition operator (+) in JavaScript
In JavaScript, the Addition operator “+” generates the sum of the numerical values specified as “operands”. It is also utilized to concatenate strings.
Syntax of Addition operator (+) in JavaScript
Example: How to use Addition operator (+) in JavaScript
In this example, the addition operator “+” will perform the addition operator for the specified number “1” and “2” and return “3” as their sum:
Output
However, adding the addition operator between a “number” and a “string” value will concatenate both values and output the resultant string.
For instance, in the below-given example, the number “1” and “apple” strings are concatenated as “1 apple” with the help of the addition operator:
Output
How to use Subtraction operator (-) in JavaScript
The JavaScript Subtraction operator “–” calculates the difference between the specified numerical values.
Syntax of Subtraction operator (-) in JavaScript
Example: How to use Subtraction (-) operator in JavaScript
Now, we will check out the difference between “5” and “3” numbers using the subtraction operator:
You can see from the output, the subtraction operation returned “2” as the difference of the specified numbers:
When the Subtraction operator is utilized in between a string and a numeric value, its return case will be set to “NaN” (Not-a-Number):
Output
How to use Multiplication operator (*) in JavaScript
The Multiplication operator “*” accepts two values as operands where the first operand is considered as “multiplicand” and the second as “multiplier”. It performs the multiplication operation on the added operands.
Syntax of Multiplication operator (*) in JavaScript
Example: How to use Multiplication operator (*) in JavaScript
When two positive numbers are multiplied using the multiplication operator “*”, the sign of the resultant value will be “+”:
Output
In case one of the operands has a negative “–” sign, then the multiplication operator will return a negative value after multiplying values:
Output
How to use Division operator (/) in JavaScript
The Division operator “/” is utilized to divide two numbers where the left operand is accepted as “dividend” and the right one as its “divisor”.
Syntax of Division operator (/) in JavaScript
Example: How to use Division operator (/) in JavaScript
Here, we will divide the number “7” with “2” and use the JavaScript division operator “/”:
The given output signifies that the result of the dividing “7/2” is “3.5”:
The division operator will return a “float” value after dividing two float type values:
Output
If zero is specified as a divisor, then the division will lead to “infinity”:
Output
How to use Modulus operator (%) in JavaScript
The JavaScript Modulus operator “%” is used to fetch the “remainder” that is left after dividing the operands. It is also called the “remainder operator“. Also, the remainder sign depends on the sign associated with the dividend.
Syntax of Modulus operator (%) in JavaScript
Example: How to use Modulus operator (%) in JavaScript
In the below-given example, the modulus operator will return “1” as “remainder” after dividing “9 / 2”:
Output
When the dividend contains a negative value, the value return by the modulus operator will also have a “–” sign:
Output
How to use Exponentiation operator (**) in JavaScript
The Exponentiation operator in JavaScript “**” raises the value of the first number to the power of the second number. It works similar to the “Math.pow” method, except the Exponentiation operator accepts BigInts as operands.
Syntax of Exponentiation operator (**) in JavaScript
Example: How to use Exponentiation operator (**) in JavaScript
We will use the exponentiation operator “**” to raise the number “6” to power “2”:
The exponentiation operator will return “36” after evaluating the given expression:
How to use Increment operator (++) in JavaScript
The Increment operator “++” adds “one” to the specified operand in JavaScript.
Syntax of increment operator (++) in JavaScript
If the increment operator is added as a “postfix”, then it will output a value before incrementing:
In the case of “prefix“, the Increment operation is performed first, and then the operator returns a value:
Example: How to use Increment operator (++) in JavaScript
Here, the increment operator “++” will add “one” to the value of “x” variable after initializing “y” with “3”:
y = x++;
console.log("x: " + x);
console.log("y: " + y);
Output
However, when increment operator “++” is used as “prefix” in the statement “y = ++x”, it will firstly increment “1” in the value of “x” variable and then initialize “y” with the updated value:
y = ++x;
console.log("x: " + x);
console.log("y: " + y);
Output
How to use Decrement operator (–) in JavaScript
The JavaScript Decrement operator “—” subtracts “one” from the specified operand.
Syntax of Decrement operator (–) in JavaScript
If the decrement operator is added as a “postfix”, then it will output a value before decrementing:
In the case of “prefix“, the decrement operation is performed first, and then the operator returns a value:
Example: How to use Decrement (–) operator in JavaScript
In the below-given example, the decrement operator “—” will subtract “one” from the value of “x” variable after initializing “y” with “3”:
y = x--;
console.log("x: " + x);
console.log("y: " + y);
Output
In the other case, if the decrement operator is specified as “postfix”, it will firstly decrement “1” from the value of “x” variable and then initialize “y” with the modified value:
y = --x;
console.log("x: " + x);
console.log("y: " + y);
Output
That was all essential information related to the basic usage of Arithmetic operators in JavaScript. Explore them according to your requirements.
Conclusion
Arithmetic operators are commonly used to perform mathematical operations. The arithmetic operators accept the numerical values as operands, which can be literals or variables, and then carry out addition, subtraction, division, multiplication, exponentiation, modulus, increment, and decrement operations. This write-up discussed the usage of Arithmetic operators in JavaScript.