JavaScript Arithmetic Operators are the operators that operate upon the numerical values and return a numerical value.
Addition (+)
The addition operator takes two numerical operands and gives their numerical sum.
Syntax:
a + b
let result = 10 + 20;
console.log(result); // Output : 30
Subtraction (-)
The subtraction operator gives the difference between two operands in the form of numerical value.
Syntax:
a - b
let result = 20 - 10;
console.log(result); // Output : 10
Multiplication (*)
The multiplication operator gives the product of operands where one operand is a multiplicand and another is a multiplier.
Syntax:
a * b
let result = 20 * 10;
console.log(result); // Output: 200
Division (/)
The division operator provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.
Syntax:
a / b
20/10 // Output: 2
1.0 / 2.0 // Output: 0.5
3.0 / 0 // Output: Infinity
Modulus (%)
The modulus operator returns the remainder left over when a dividend is divided by a divisor.
Syntax:
a % b
20%10 // Output: 0
9 % 5 // Output: 4
NaN % 2 // Output: NaN
Exponentiation (**)
The exponentiation operator gives the result of raising the first operand to the power of the second operand.
Syntax:
a ** b
2 ** 5 // Output: 25
3 ** 3 // Output: 27
NaN ** 2 // Output: NaN
Increment (++)
The increment operator increments (adds one to) its operand and returns a value.
If used postfix with operator after operand (for example, x++), then it increments and returns the value before incrementing.
If used prefix with the operator before operand (for example, ++x), then it increments and returns the value after incrementing.
Syntax:
a++ or ++a
// Postfix
var x = 5;
y = x++; // Output : x = 6, y = 5
// Prefix
var a = 5;
b = ++a; // Output : a = 6, b = 6
Decrement (–)
If used postfix, with operator after operand (for example, x–), then it decrements and returns the value before decrementing.
If used prefix, with the operator before operand (for example, –x), then it decrements and returns the value after decrementing.
Syntax:
a-- or --a
// Postfix
var x = 5;
y = - -x; //Output: x = 4, y = 4
// Prefix
var a = 5;
b = a- -; // Output : a = 4, b = 5