Mathematical Operators

For a mathematical calculation you use a mathematical operator. You can use as value(s) one, two, or more numbers, variables, or numbers and variables.

As a quick example let’s use the mathematical operator + to add two values that are both variables.


var BeginText="I have a ";
var EndText="black Honda PCX.";
window.alert(BeginText + EndText);

Below is The Mathematical Operators table with their symbols and their functions.

OperatorSymbolFunction
Addition+Adds two values
SubstractionSubstract one value from another
Multiplication*Multiplies two values
Division/Divides one value by another
Modulus%Divides one value by another and returns the remainder
Increment++Shortcut to add 1 to a single number
DecrementShortcut to substract 1 from a single number
Unary negationMakes a positive negative or a negative positive

The Addition Operator (+)

The Addition Operator in JavaScript is used to add numbers in mathematical calculations or to combine two strings.

For example you can add two numbers to get the result:


var number1=5;
var number2=3;
var TheSum = number1 + number2;
window.alert( TheSum);

The Subtraction Operator (-)

As you learn at Mathematics, the Subtraction Operator in JavaScript is used to subtract the value on its right side from the value on its left side.


var number1 = 5;
var number2 = 3;
var TheSum = number1 - number2;
window.alert(TheSum);

The Multiplication Operator (*)

The Multiplication Operator in JavaScript is used to multiply the value on its right side by the value on its left side.


var number1 = 5;
var number2 = 3;
var TheSum = number1 * number2;
window.alert(TheSum);

The Division Operator (/)

The Division Operator in JavaScript is used to divide the value on its left side by the value on its right side.


var number1 = 5;
var number2 = 3;
var TheSum  = number1 / number2;
window.alert(TheSum);

The Modulus Operator (%)

The Modulus Operator in JavaScript is used to divide the value on its left side by the value on its right side then give a result that is the integer remainder of the division.


var number1 = 5;
var number2 = 3;
var TheSum = number1 % number2;
window.alert(TheSum);

In our example, 5 divided by 3 is 1 and a reminder of 2. The reminder 2 is the result given by the Modulus Operator. If the reminder is 0 then the result would be 0.

The Increment Operator (++)

The Increment Operator can be used on either side of the operand (the value on which it operates). All it does is to increase the value of the operand by 1.

When the Increment Operator is placed before the operand, the value of the operand is increased by 1 and the rest of the statement is executed.


var number1 = 5;
var TheResult = ++ number1;
window.alert(TheResult);

In this example the value of the variable number1 is increased by 1 so it is set to 6.

If the Increment Operator is used after the operand it changes the value of the operand after assignment.


var number1 = 5;
var TheResult = number1++;
window.alert(TheResult);
window.alert(number1 + TheResult);

The first alert window.alert(TheResult); will give you the result of 5 but the second alert window.alert(number1 + TheResult); will give you the result of 11 which is 5+6.

The Decrement Operator (–)

The Decrement Operator works just like the Increment Operator but it subtracts 1 from the operand rather than adding 1 to it.


var number1 = 5;
var TheResult = --number1;
window.alert(TheResult);

When the Decrement Operator is placed after the operand, the rest of the statement is executed and the operand is decremented afterward.


var number1 = 5;
var TheResult = number1--;
window.alert(TheResult);
window.alert(number1 + TheResult);

The first alert window.alert(TheResult); will give you the result of 5 but the second alert window.alert(number1 + TheResult); will give you the result of 9 which is 5+4.

The Unary Negation Operator (-)

The Unary Negation Operator negates the current sign of a number (positive or negative).


var NegativeNumber = -3;

This defines a variable with the value of negative 3. The operator tells the browser that the value of 3 is not positive.

You can use the Unary Negation Operator as is shown below:


var NegativeNumber = 4 + (-3);

var Number1 = 5;
var Number2 = 3;
var Number3 = -Number2;
window.alert(Number1 + Number2 + Number3);

Leave a Reply