loading

JS Arithmetic

JavaScript Arithmetic Operators

Arithmetic operators perform operations on integers (literals or variables).

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

Arithmetic Operations

A typical arithmetic procedure works with two numbers.

The two numbers could be literals.

Example

				
					let x = 100 + 50;
				
			

or variables:

Example

				
					let x = a + b;
				
			

or expressions:

Example

				
					let x = (100 + 50) * a;
				
			

Operators and Operands

The numbers used in an arithmetic operation are referred to as operands.

An operator defines the operation that will be done between the two operands.

OperandOperatorOperand
100+50

Adding

To add numbers, use the addition operator (+):

Example

				
					let x = 5;
let y = 2;
let z = x + y;
				
			

Subtracting

The subtraction operator () subtracts values.

Example

				
					let x = 5;
let y = 2;
let z = x - y;
				
			

Multiplying

The multiplication operator (*) multiplyes numbers.

Example

				
					let x = 5;
let y = 2;
let z = x * y;
				
			

Dividing

The division operator (/) divides numbers.

Example

				
					let x = 5;
let y = 2;
let z = x / y;
				
			

Remainder

The modulus operator (%) returns the division remainder.

Example

				
					let x = 5;
let y = 2;
let z = x % y;
				
			

In arithmetic, dividing two integers results in a quotient and a remainder.

In mathematics, a modulo operation yields the remainder of an arithmetic division.

Incrementing

The increment operator (++) increments numbers.

Example

				
					let x = 5;
x++;
let z = x;
				
			

Decrementing

The decrement operator () decrements numbers.

Example

				
					let x = 5;
x--;
let z = x;
				
			

Exponentiation

The exponentiation operator (**) multiplies the first operand to the power of the second.

Example

				
					let x = 5;
let z = x ** 2;
				
			

x ** y yields the same output as Math.pow(x,y):

Example

				
					let x = 5;
let z = Math.pow(x,2);
				
			

Operator Precedence

Operator precedence refers to the order in which operations are performed in an arithmetic expression.

Example

				
					let x = 100 + 50 * 3;
				
			

Is the result of the preceding example the same as 150 * 3, or 100 + 150?

Is addition or multiplication done first?

Multiplication comes first, as it does in regular school mathematics.

Multiplication (*) and division (/) take precedence over addition and subtraction.

In addition, parenthesis can be used to shift the precedence, just like in school mathematics.

When employing parentheses, the operations inside them are computed first:

Example

				
					let x = (100 + 50) * 3;
				
			

When multiple operations have the same priority (such as addition and subtraction or multiplication and division), they are computed from left to right.

Example

				
					let x = 100 + 50 - 3;
				
			
				
					let x = 100 / 50 * 3;
				
			
Share this Doc

JS Arithmetic

Or copy link

Explore Topic