To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Mathematics in JavaScript

Including mathematics within a program is a simple task. JavaScript makes it easy to create a mathematical expression. This part of the tutorial will describe how to create a mathematical expression and evaluate it.

Mathematics in JavaScript

The following code sample shows how to store a Number value in a variable:

JavaScript
var x = 10;
var y = 12;

Variables are not completely necessary in a mathematical expression, but for each of these examples they will be used to store the inputs and the outputs.

With these two variables, two numbers have been stored. For each of the following code samples these variables will be used.

Mathematical operations

This is a list of all of the mathematical operations that can be carried out in JavaScript as well as examples of them.

Addition and subtraction

The addition operator is the + symbol:

JavaScript
var x = 10;
var y = 12;
var z = x + y;
alert(z);

Subtraction uses the - symbol:

JavaScript
var x = 10;
var y = 12;
var z = x - y;
alert(z);

Multiplication and division

Multiplication in JavaScript uses the asterisk symbol *:

JavaScript
var x = 10;
var y = 12;
var z = x * y;
alert(z);

And division uses the forward slash / symbol:

JavaScript
var x = 10;
var y = 12;
var z = x / y;
alert(z);

Modular arithmetic

A final mathematical operator known as modulus performs a less well-known operation. What modulus does is it divides two numbers and returns the remainer of those two numbers. Take 10 and 12, if 12 is divided by 10 it leaves a remainder of 2:

JavaScript
var x = 10;
var y = 12;
var z = y % x;
alert(z);

Increment and decrement

The final mathematical operation that this page discusses is the increment and decrement operations. Incrementing means increasing in steps whereas decrementing means decreasing in steps.

The increment operator is ++. In the following example the value will increment after each alert. This is called post incrementing:

JavaScript
var x = 10;
alert(x++);
alert(x++);

The next example will show the value being increased before each alert. This is called pre incrementing:

JavaScript
var x = 10;
alert(++x);
alert(++x);

Decrement is the same except that the symbol changes to --. The following is an example of post decrementing:

JavaScript
var x = 10;
alert(x--);
alert(x--);

The following is an example of pre decrementing:

JavaScript
var x = 10;
alert(--x);
alert(--x);

All of these examples increase or decrease by just one. To increase or decrease a value by another number, for instance 3, a different symbol is used:

JavaScript
var x = 10;

//Add and Assignment
x += 3;
alert(x);

//Subtract and Assignment
x -= 3;
alert(x);

//Multiply and Assignment
x *= 3;
alert(x);

//Divide and Assignment
x /= 3;
alert(x);

As shown in the comments, this is called the operation and assignment, since what happens is the operation is carried out and then it is assigned to the variable.

Each of these operations are the equivalent to it's mathematical operation described previously.

Provide feedback on this page
Comments are sent via email to me.