(Also see the topic on the Math.objects.)
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).
Produces the sum of numeric operands or string concatenation.
// Number + Number -> addition
1 + 2 // 3
// Boolean + Number -> addition
true + 1 // 2
// Boolean + Boolean -> addition
false + false // 0
// Number + String -> concatenation
5 + "foo" // "5foo"
// String + Boolean -> concatenation
"foo" + false // "foofalse"
// String + String -> concatenation
"foo" + "bar" // "foobar"
Produces the difference between two operands by subtracting them
5 - 3 // 2
3 - 5 // -2
"foo" - 3 // NaN (not a number error)
Produces the quotient of its operands (left operand is the dividend; right operand is the divisor)
1 / 2 // returns 0.5
// (neither number is explicitly a floating point number)
1.0 / 2.0 // returns 0.5
2.0 / 0 // returns Infinity
2.0 / 0.0 // also returns Infinity
2.0 / -0.0 // returns -Infinity
Produces the result of the product of the operands.
2 * 2 // 4
-2 * 2 // -4
Infinity * 0 // NaN (not a number error)
Infinity * Infinity // Infinity
"foo" * 2 // NaN
Returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor.
12 % 5 // 2
-1 % 2 // -1
1 % -2 // 1
NaN % 2 // NaN (not a number error)
1 % 2 // 1
2 % 3 // 2
-4 % 2 // -0
5.5 % 2 // 1.5
The exponentiation operator returns the result of raising first operand to the power of the second operand.
-2 ** 2; // Invalid in JavaScript, as the operation is ambiguous
-(2 ** 2); // -4 in JavaScript (the author"s intention is unambiguous)
Increments its operand by 1 and returns a value. Can be postfix, where it shows the value before incrementing; or prefix, where it shows the value after incrementing; or (see examples):
// Postfix
var x = 3;
y = x++; // y = 3, x = 4
// Prefix
var a = 2;
b = ++a; // a = 3, b = 3
Decrements is operand by 1 (subtracts 1) and returns a value. Can be postfix, where it shows the value before decrementing; or prefix, where it shows the value after decrementing; or (see examples):
// Postfix
var x = 3;
y = x--; // y = 3, x = 2
// Prefix
var a = 2;
b = --a; // a = 1, b = 1
Precedes its operand and negates it.
var x = 3;
y = -x; // y = -3, x = 3
//unary negation operator can convert non-numbers into a number
var x = "4";
y = -x; // y = -4
The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn"t already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. If it cannot parse a particular value, it will evaluate to NaN (not a number error).
+3 // 3
+"3" // 3
+true // 1
+false // 0
+null // 0
+function(val){ return val } // NaN