Main Menu

Categories

Subscribe

Arithmetic Operators in JavaScript

April 4th, 2009

Addition Operator (+)

The addition operator adds numbers together and also joins strings together.

document.write(2 + 2);

Subtraction Operator (-)

Subtracts two or more numbers .

document.write(5 - 3);

Multiplication Number (*)

Multiplies two or more numbers.

document.write(5 * 2);

Division Operator (/)

The Division Operator divides its first operand by its second. If an outputted result could be a floating point number (4.5), JavaScript will return a floating point number and not an integer.

document.write(9/2);

Modulo Operator (%)

The Modulo operator returns the remainder when the first operand is divided by the second operand.

document.write(9%2);

Unary Operator (-)

When the subtraction operator is used before an operand, it converts a positive value to a negative value.

var one = 1;
document.write(-one);

Unary Operator (+)

When the addition operator is used before an operand, it converts a negative value to a positive value.

var one = -1;
document.write(+one);

Increment Operator (++)

The Increment Operator increments, adds 1 to a variable value. If (++) is used before the operand, it increments and evaluates the operand to the incremented value. If used after the operand, it increments the operand but evaluates to the incremented value of that operand.

var ten = 10;
document.write(++ten);

Decrement Operator (–)

The Decrement Operator decrements, subtracts 1 to a variable value. If (–) is used before the operand, it decrements and evaluates the operand to the decremented value. If used after the operand, it decrements the operand but evaluates to the decremented value of that operand.

var ten = 10;
document.write(--ten);

No Comments

Leave a Reply