JavaScript Reference ยท Operators
JavaScript arithmetic operators
Reference for JavaScript arithmetic operators: addition, subtraction, multiplication, division, remainder, exponent, increment and decrement, with examples.
Arithmetic operators do math on numbers. JavaScript has the usual four (+, -, *, /), the remainder operator %, the exponent operator **, and the unary operators +, -, ++ and --. Numbers are 64-bit floating-point values, so a few results (such as 0.1 + 0.2) are not exact.
The + operator is special: if either operand is a string, it concatenates instead of adding. All the other arithmetic operators convert their operands to numbers first.
a + b a - b a * b a / b a % b a ** b +a -a a++ ++a a-- --a
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
x + y // addition or string concatenation x - y // subtraction x * y // multiplication x / y // division x % y // remainder x ** y // exponentiation +x // unary plus (converts to number) -x // unary negation x++ // postfix increment ++x // prefix increment x-- // postfix decrement --x // prefix decrement
| Operator | Description |
|---|---|
+ |
Adds two numbers. If either operand is a string (after converting objects to primitives), joins them as text. |
-, *, / |
Convert both operands to numbers and compute the result. |
% |
Remainder after division. The result has the sign of the left operand. |
** |
Raises the left operand to the power of the right one. It is right-associative: 2 ** 3 ** 2 is 2 ** 9. |
++, -- |
Add or subtract 1 from a variable or property. The postfix form returns the old value, the prefix form the new one. |
unary +, - |
Convert to a number; - also flips the sign. |
Return value: a number, except for + with a string operand (a string) and for BigInt operands (a BigInt).
Exceptions: mixing a BigInt with a regular number throws a TypeError. A BigInt division by zero or % by zero throws a RangeError. Unary + on a BigInt throws a TypeError. A negative base with ** needs parentheses: -2 ** 2 is a SyntaxError, so write (-2) ** 2.
Usage notes
- Division by zero does not throw for regular numbers: you get
Infinity,-InfinityorNaN. - Floating-point error:
0.1 + 0.2 !== 0.3. Compare with a tolerance, or work in integers (cents instead of dollars). %is a remainder, not a modulo.-1 % 5is-1. For a result that is always non-negative, use((n % m) + m) % m.NaNspreads: any arithmetic withNaNgivesNaN. Check withNumber.isNaN().- Concatenation trap:
"5" + 2is"52", but"5" - 2is3. Convert explicitly withNumber()when reading user input. - Increment style:
i++and++idiffer only when you use the returned value. On their own statement they do the same. - Overflow: integers are exact only up to
Number.MAX_SAFE_INTEGER(2^53 - 1). Use BigInt beyond that. - Compound forms such as
+=and**=are covered on the assignment operators page.
Browser support
Supported in every browser and runtime. The exponent operator ** is part of ES2016 and is available in all current browsers.