JavaScript Arithmetic

JavaScript arithmetic operators are used to perform arithmetic operations on numbers (like variables or literals). An arithmetic operation is performed on 2 numbers typically. These numbers are known as an operand and the operation is done by an operator. Following are some basic JavaScript arithmetic operators:

OperatorDescription
+Addition
Subtraction
*Multiplication
**Exponentiation (ES2016)
/Division
%Modulus (Remainder)
++Increment
Decrement

In the below example 10 and 2 are two operands and * is used as an operator.

10 * 2

Following example shows some JavaScript arithmetic operations:

let x = 7;
let y = 3;

let z = x + y;  	// result is 10

let z = x - y; 	        // result is 4

let z = x * y; 	        // result is 21

let z = x / y; 	        // result is 2.3333333333333335

let z = x % y;  	// result is 1

x++;			// result is 8

x--;			// result is 6

let z = x ** 2;         // result is 49

JavaScript Arithmetic – Operators Precedence

The order of performing operations on arithmetic expression is known as precedence of the operator. It describes the sequence of performing on an arithmetic expression.

15/Division10 / 5
15%Division Remainder10 % 5
14+Addition10 + 5
14Subtraction10 – 5
13<< Shift leftx << 2
13>> Shift rightx >> 2
13>>> Shift right (unsigned)x >>> 2
12Less thanx < y 
12<=Less than or equalx <= y
12Greater thanx > y
12>=Greater than or equalx >= y
12inProperty in Object“PI” in Math
12instanceofInstance of Objectinstanceof Array
11==Equalx == y
11===Strict equalx === y
11!=Unequalx != y
11!==Strict unequalx !== y
10&Bitwise ANDx & y
9^Bitwise XORx ^ y
8|Bitwise ORx | y
7&&Logical ANDx && y
6||Logical ORx || y
5??Nullish Coalescingx ?? y
4? :Condition? “Yes” : “No”
3+=Assignmentx += y
3/=Assignmentx /= y
3-=Assignmentx -= y
3*=Assignmentx *= y
3%=Assignmentx %= y
3<<=Assignmentx <<= y
3>>=Assignmentx >>= y
3>>>=Assignmentx >>>= y
3&=Assignmentx &= y
3^=Assignmentx ^= y
3|=Assignmentx |= y
2yieldPause Functionyield x
1,Comma5 , 6

In the above table, bold entries indicate ECMAScript 2015 (ES6) or higher.

<!DOCTYPE html>
<html>
<body>

<p id="tacode"></p>

<script>
let p = 10 + 4 * 2 / 2 * 4 ^ 7; 
document.getElementById("tacode").innerHTML = p;
</script>

</body>
</html>

Output:

JavaScript Arithmetic

In the above example, multiplication will be performed first then addition will be done because multiplication has higher precedence.