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 (/).
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Addition (+)
The addition operator produces the sum of numeric operands or string concatenation.
Syntax
Operator: x + y
Examples
// 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"
Subtraction (-)
The subtraction operator subtracts the two operands, producing their difference.
Syntax
Operator: x - y
Examples
5 - 3 // 2 3 - 5 // -2 'foo' - 3 // NaN
Division (/)
The division operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
Syntax
Operator: x / y
Examples
1 / 2 // returns 0.5 in JavaScript 1 / 2 // returns 0 in Java // (neither number is explicitly a floating point number) 1.0 / 2.0 // returns 0.5 in both JavaScript and Java 2.0 / 0 // returns Infinity in JavaScript 2.0 / 0.0 // returns Infinity too 2.0 / -0.0 // returns -Infinity in JavaScript
Multiplication (*)
The multiplication operator produces the product of the operands.
Syntax
Operator: x * y
Examples
2 * 2 // 4 -2 * 2 // -4 Infinity * 0 // NaN Infinity * Infinity // Infinity 'foo' * 2 // NaN
Remainder (%)
The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
Syntax
Operator: var1 % var2
Examples
12 % 5 // 2 -1 % 2 // -1 1 % -2 // 1 NaN % 2 // NaN 1 % 2 // 1 2 % 3 // 2 -4 % 2 // -0 5.5 % 2 // 1.5
Exponentiation (**)
The exponentiation operator returns the result of raising first operand to the power second operand. That is, var1var2, in the preceding statement, where var1 and var2 are variables. Exponentiation operator is right associative. a ** b ** c is equal to a ** (b ** c).
Syntax
Operator: var1 ** var2
Notes
In most languages like PHP and Python and others that have an exponentiation operator (**), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators. In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (+/-/~/!/delete/void/typeof) immediately before the base number.
-2 ** 2; // 4 in Bash, -4 in other languages. // This is invalid in JavaScript, as the operation is ambiguous. -(2 ** 2); // -4 in JavaScript and the author's intention is unambiguous.
Examples
2 ** 3 // 8 3 ** 2 // 9 3 ** 2.5 // 15.588457268119896 10 ** -1 // 0.1 NaN ** 2 // NaN 2 ** 3 ** 2 // 512 2 ** (3 ** 2) // 512 (2 ** 3) ** 2 // 64
To invert the sign of the result of an exponentiation expression:
-(2 ** 2) // -4
To force the base of an exponentiation expression to be a negative number:
(-2) ** 2 // 4
Note: JavaScript also has a bitwise operator ^ (logical XOR). ** and ^ are different (for example : 2 ** 3 === 8 when 2 ^ 3 === 1.)
Increment (++)
The increment operator increments (adds one to) its operand and returns a value.
- If used postfix, with operator after operand (for example, x++), then it increments and returns the value before incrementing.
- If used prefix, with operator before operand (for example, ++x), then it increments and returns the value after incrementing.
Syntax
Operator: x++ or ++x
Examples
// Postfix var x = 3; y = x++; // y = 3, x = 4 // Prefix var a = 2; b = ++a; // a = 3, b = 3
Decrement (--)
The decrement operator decrements (subtracts one from) its operand and returns a value.
- If used postfix, with operator after operand (for example, x--), then it decrements and returns the value before decrementing.
- If used prefix, with operator before operand (for example, --x), then it decrements and returns the value after decrementing.
Syntax
Operator: x-- or --x
Examples
// Postfix var x = 3; y = x--; // y = 3, x = 2 // Prefix var a = 2; b = --a; // a = 1, b = 1
Unary negation (-)
The unary negation operator precedes its operand and negates it.
Syntax
Operator: -x
Examples
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
Unary plus (+)
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, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.
Syntax
Operator: +x
Examples
+3 // 3
+'3' // 3
+true // 1
+false // 0
+null // 0
+function(val){ return val } // NaN
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) | Draft | |
| ECMAScript 2016 (ECMA-262) | Standard | Added Exponentiation operator. |
| ECMAScript 2017 (ECMA-262) | Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) | Standard | Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators. |
| ECMAScript 5.1 (ECMA-262) | Standard | Defined in several sections of the specification: Additive operators, Multiplicative operators, Postfix expressions, Unary operators. |
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition (+) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Decrement (--) | Chrome Full support 2 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Division (/) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Exponentiation (**) | Chrome Full support 52 | Edge Full support 14 | Firefox Full support 52 | IE No support No | Opera Full support Yes | Safari Full support 10.1 | WebView Android Full support 51 | Chrome Android Full support 52 | Firefox Android Full support 52 | Opera Android Full support Yes | Safari iOS Full support 10.3 | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.0.0
|
Increment (++) | Chrome Full support 2 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Multiplication (*) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Remainder (%) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Subtraction (-) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Unary negation (-) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Unary plus (+) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.