JavaScript Tutorial JavaScript References

JavaScript - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), exponent(**), and modulo(%) operators.

var X = 11;
var Y = 2;
var txt;

txt = "X = " + X + ", Y = "+ Y + "<br><br>";

//Add X and Y
txt = txt + "X + Y = " + (X + Y) + "<br>";

//Subtract Y from X
txt = txt + "X - Y = " + (X - Y) + "<br>";

//Multiply X with Y
txt = txt + "X * Y = " + (X * Y) + "<br>";

//Divide X by Y
txt = txt + "X / Y = " + (X / Y) + "<br>";

//X raised to the power of Y
txt = txt + "X ** Y = " + (X ** Y) + "<br>";

//Remainder when X is divided by Y
txt = txt + "X % Y = " + (X % Y) + "<br>";

The output (value of txt) after running above script will be:

X = 11, Y = 2

X + Y = 13
X - Y = 9
X * Y = 22
X / Y = 5.5
X ** Y = 121
X % Y = 1

❮ JavaScript - Operators