JavaScript arithmetic

-

JavaScript Arithmetic Operators

Arithmetic operators perform arithmetic on numbers (literals or variables).

Operator Description
- Subtraction
* Multiplication
** Exponentiation (ES6)
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement

Arithmetic Operations

A typical arithmetic operation operates on two numbers.

The two numbers can be literals:

var x = 100 + 50;

or variables:

var x = a + b;

or expressions:

var x = (100 + 50) * a;

Operators and Operands

The numbers (in an arithmetic operation) are called operands.

The operation (to be performed between the two operands) is defined by an operator.

Operand Operator Operand
100 + 50

var x = 5;
var y = 2;
var z = x + y;   // Result 7

Subtracting

The subtraction operator (-) subtracts numbers.

var x = 5;
var y = 2;
var z = x - y;    // Result 3

Multiplying

The multiplication operator (*) multiplies numbers.

var x = 5;
var y = 2;
var z = x * y;    // Result 10

Dividing

The division operator (/) divides numbers.

var x = 5;
var y = 2;
var z = x / y;   // Result 2.5

Remainder

The modulus operator (%) returns the division remainder.

var x = 5;
var y = 2;
var z = x % y;   // Result 1

In arithmetic, the division of two integers produces a quotient and a remainder.
In mathematics, the result of a modulo operation is the remainder of an arithmetic division.

Incrementing

The increment operator (++) increments numbers.

var x = 5;
x++;   // This will increase the value of x by 1
var z = x;  // Result 6

Decrementing

The decrement operator (--) decrements numbers.

var x = 5;
x--;   // This will decrease the value of x by 1
var z = x;  // Result 4

Exponentiation

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

var x = 5;
var z = x ** 2;          // Result 25

x ** y produces the same result as Math.pow(x,y):

var x = 5;
var z = Math.pow(x,2);   // Result 25

Operator Precedence

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

var x = 100 + 50 * 3;  // Result 250

Is the result of example above the same as 150 * 3, or is it the same as 100 + 150?

Is the addition or the multiplication done first?

As in traditional school mathematics, the multiplication is done first.

Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).

And (as in school mathematics) the precedence can be changed by using parentheses:

var x = (100 + 50) * 3;   // Result 450

When using parentheses, the operations inside the parentheses are computed first.

When many operations have the same precedence (like addition and subtraction), they are computed from left to right:

var x = 100 + 50 - 3;  // Result 147

Exercises - test your knowledge

Exercise 1

Multiply 10 with 5, and alert the result:

Exercise 2

Divide 10 by 2, and alert the result:

Exercise 3

Alert the remainder when 15 is divided by 9.

alert (15 % 9);

Exercise 4

x = 10;

y = 5

Use the correct assignment operator that will result in x being 15 (same as x = x + y).

x = 10;
y = 5;
x += y;

Exercise 5

x = 10;

y = 5

Use the correct assignment operator that will result in x being 50 (same as x = x * y).

x = 10;
y = 5;
x *= y

Rate this lesson 