JavaScript Arithmetic Operators: +, -, *, /, %, **, Increment and Decrement
Every arithmetic operator in depth: floating-point division, the remainder operator and negative numbers (plus a true modulo), exponentiation, the + operator's add-or-concatenate algorithm, unary plus and minus as conversions, prefix vs postfix ++ and --, and BigInt arithmetic rules.
kvi5hnuCode Sprout team member8 min readOn this page
JavaScript's arithmetic operators look like the ones on a calculator, and most of the time they behave that way. The surprises come from three places: all numbers are floating point (article 12), operands are converted automatically (article 23), and + has a second job, joining strings. This article goes through every arithmetic operator with those surprises in mind.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition or string concatenation | 7 + 2 | 9 |
- | subtraction | 7 - 2 | 5 |
* | multiplication | 7 * 2 | 14 |
/ | division | 7 / 2 | 3.5 |
% | remainder | 7 % 2 | 1 |
** | exponentiation (ES2016) | 7 ** 2 | 49 |
+x / -x | unary plus / minus | -"7" | -7 |
++ / -- | increment / decrement | n++ | adds 1 to n |
The simple four: -, *, / and %
-, *, /, % and ** all follow one rule: convert both operands to numbers, then compute. A value that can't be converted becomes NaN, and NaN in any operand makes the result NaN:
console.log("12" - 2, "3" * "4", "10" / "4");
console.log("abc" * 2, undefined - 1, null * 5);
console.log(true * 10, [] - 1, [6] / 2);
10 12 2.5
NaN NaN 0
10 -1 3
null becomes 0, undefined becomes NaN, true becomes 1, and arrays go through their string form ([] → "" → 0, [6] → "6" → 6). Article 22 has the full conversion table.
Division is always floating point
There's no integer division operator. 7 / 2 is 3.5, never 3. Dividing by zero doesn't throw either:
console.log(7 / 2, 1 / 0, -1 / 0, 0 / 0);
console.log(Math.trunc(-7 / 2), Math.floor(-7 / 2)); // two kinds of "integer division"
3.5 Infinity -Infinity NaN
-3 -4
For integer division, choose deliberately: Math.trunc rounds toward zero (like C and Java), Math.floor rounds toward negative infinity (like Python's //). They differ only for negative results.
The remainder operator %
a % b gives what's left after dividing a by b a whole number of times. The sign of the result follows the first operand (the dividend):
console.log(7 % 3, -7 % 3, 7 % -3, -7 % -3);
console.log(5.5 % 2, 0.3 % 0.1); // works with fractions, with float error
1 -1 1 -1
1.5 0.09999999999999998
That makes % a remainder, not the mathematical modulo that some languages have (Python's -7 % 3 is 2). The difference bites when you wrap around, as in cycling through an array backwards or computing clock hours:
const mod = (n, m) => ((n % m) + m) % m;
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const today = 1; // Monday
console.log(days[(today - 3) % 7]); // index -2: undefined!
console.log(days[mod(today - 3, 7)]); // index 5
console.log(mod(-1, 12), mod(13, 12));
undefined
Fri
11 1
A common use of % is testing divisibility: n % 2 === 0 for even numbers. Be careful with the odd test: n % 2 === 1 is false for negative odd numbers (-3 % 2 is -1). Use n % 2 !== 0.
Exponentiation: **
a ** b raises a to the power b. It's the same as Math.pow(a, b), but it's an operator, so it also works with BigInt:
console.log(2 ** 10, 9 ** 0.5, 2 ** -1);
console.log(2 ** 3 ** 2); // right-associative: 2 ** 9
console.log((-8) ** (1 / 3), Math.cbrt(-8));
console.log(2 ** 1024, 2n ** 64n);
1024 3 0.5
512
NaN -2
Infinity 18446744073709551616n
- Fractional exponents compute roots, but a negative base with a fractional exponent is
NaN(the result would be complex). UseMath.cbrtfor cube roots of negatives. **is right-associative, and-2 ** 2is a SyntaxError (article 30).x **= 2squares a variable in place.
The + operator: add or concatenate
+ is the one arithmetic operator that also works on strings. The rule is simple once you know it: if either operand is a string (after converting objects to primitives), concatenate; otherwise add numbers.

console.log(7 + 2, "7" + 2, 7 + "2");
console.log(true + true, null + 1, undefined + 1);
console.log(1 + {}, [] + [], [1, 2] + [3]);
console.log(4 + 3 + "px", "px" + 4 + 3);
9 72 72
2 1 NaN
1[object Object] 1,23
7px px43
Line by line:
- A string on either side means concatenation, whichever side it's on.
- No strings: booleans,
nullandundefinedconvert to numbers. - Objects first become primitives (article 24): a plain object becomes
"[object Object]", and arrays become their elements joined by commas.[] + []is the empty string, which is why the output shows two spaces. +groups left to right, so the position of the first string matters (article 30).
The classic form-input bug
Values from form fields, URL parameters, localStorage and prompt() are strings. Adding to them concatenates:
const quantityField = "3"; // what input.value gives you
console.log(quantityField + 1); // "31"
console.log(Number(quantityField) + 1); // convert first
console.log(quantityField - 1); // - always converts: works, but by accident
31
4
2
Convert explicitly at the point where the input enters your program (Number(...), or input.valueAsNumber for numeric inputs), and the rest of your code can trust its types. TypeScript catches this whole class of bug at compile time (part 17).
Building strings: + or template literals?
For anything more than joining two values, template literals (article 16) are clearer and avoid precedence surprises:
const total = 4 + 3;
console.log("Total: " + total + " items"); // fine
console.log("Sum: " + 4 + 3); // "Sum: 43", a bug
console.log(`Sum: ${4 + 3}`); // "Sum: 7"
Unary plus and minus
Unary plus (+x) converts its operand to a number and does nothing else. It's the shortest conversion, with exactly the same rules as Number(x):
console.log(+"", +" 12 ", +"0x1A", +"1e3", +"12px");
console.log(+[], +[5], +[1, 2], +{}, +null, +undefined, +true);
try {
+1n;
} catch (err) {
console.log(err.name); // BigInt can't silently become a number
}
0 12 26 1000 NaN
0 5 NaN NaN 0 NaN 1
TypeError
Unary minus (-x) converts the same way and then flips the sign: -"3" is -3. Note that -0 is a real value (article 11).
Increment and decrement: ++ and --
++ adds 1 to a variable, property or array element (an lvalue, article 30) and stores the result back. -- subtracts 1. Each has two forms, and the difference is the value of the expression:
- Prefix
++x: increment first, then evaluate to the new value. - Postfix
x++: evaluate to the old value, then increment.
let p = 5;
const pre = ++p; // p becomes 6, pre is 6
let q = 5;
const post = q++; // post is 5, then q becomes 6
console.log(pre, p, post, q);
const queue = ["a", "b", "c"];
let next = 0;
console.log(queue[next++], queue[next++], next); // use, then advance
6 6 5 6
a b 2
When the result isn't used (count++; on its own line, or the update part of a for loop), the two forms are identical. Many style guides prefer count += 1 for clarity; ESLint's no-plusplus rule exists for teams that want to ban ++ entirely.
++ always converts to a number
x++ is not the same as x = x + 1. ++ never concatenates:
let a = "1";
let b = "1";
a++;
b = b + 1;
console.log(a, typeof a, b, typeof b);
let neverSet;
neverSet++;
const stats = {};
stats.hits++; // undefined + 1
console.log(neverSet, stats.hits);
const tally = {};
tally.hits = (tally.hits ?? 0) + 1; // the safe counting pattern
console.log(tally.hits);
2 number 11 string
NaN NaN
1
The stats.hits++ line is a common bug when counting things in an object: the first increment turns a missing property into NaN, and NaN stays NaN forever. Initialize the count, or use ?? 0 as shown. Part 11 shows a cleaner tool for counting: Map with getOrInsert, added in ES2026.
Other rules:
- The operand must be assignable:
5++and(a + b)++are SyntaxErrors, and++on aconstthrows a TypeError. - No line break is allowed between an operand and a postfix
++/--(article 8). ++works on BigInt (let n = 10n; n++gives11n).
BigInt arithmetic
All arithmetic operators except unary + work with BigInt (article 13), with three extra rules:
console.log(7n / 2n, -7n / 2n, 7n % -2n); // division truncates toward zero
for (const code of ["5n + 2", "1n / 0n", "2n ** -1n"]) {
try {
eval(code);
} catch (err) {
console.log(code.padEnd(9), err.name);
}
}
console.log(5n + BigInt(2), Number(5n) + 2);
3n -3n 1n
5n + 2 TypeError
1n / 0n RangeError
2n ** -1n RangeError
7n 7
- Division truncates to an integer, rounding toward zero; there are no fractions.
- No mixing with numbers: convert one side explicitly with
BigInt()orNumber(). - Things that would produce a non-integer or infinity throw a RangeError instead of giving
InfinityorNaN.
Common questions
Why is 0.1 * 3 equal to 0.30000000000000004?
Because 0.1 can't be stored exactly in binary floating point; the tiny error becomes visible after the multiplication. Article 12 explains why, and how to handle money (work in integer cents, or format with toFixed / Intl.NumberFormat).
Does JavaScript have integer overflow?
Not like C or Java. Numbers are doubles: past Number.MAX_SAFE_INTEGER (2^53 − 1) they lose precision silently (2 ** 53 + 1 === 2 ** 53 is true), and past about 1.8e308 they become Infinity. BigInt has no upper limit except memory.
Why doesn't x / 0 throw an error?
IEEE 754 floating point defines division by zero as Infinity or -Infinity (and 0 / 0 as NaN), and JavaScript follows the standard. Check results with Number.isFinite(result) when division by zero is possible. BigInt division by 0n does throw.
Is x += 1 the same as x++?
As a statement, almost: both add 1 if x is a number. They differ if x is a string: x += 1 concatenates ("1" becomes "11"), while x++ converts to a number ("1" becomes 2). As expressions, x += 1 evaluates to the new value, like ++x.
Summary
-,*,/,%and**always convert both operands to numbers; anything unconvertible givesNaN./is floating-point division; useMath.truncorMath.floorfor integer division. Dividing by zero gives±InfinityorNaN, not an error.%is a remainder whose sign follows the dividend; use((n % m) + m) % mfor a wrapping modulo.**is right-associative, works with BigInt and needs parentheses around a negative base.+concatenates if either operand (after object-to-primitive conversion) is a string, otherwise adds. Convert input strings explicitly.- Unary
+converts to a number likeNumber()(but throws on BigInt); unary-converts and negates. - Prefix
++xgives the new value, postfixx++the old one;++never concatenates, andundefined++isNaN. - BigInt arithmetic truncates, refuses to mix with numbers, and throws
RangeErrorfor division by zero and negative exponents.
Practice Questions
What does "5" * "2" + "5" - 2 evaluate to?
Answer
103. "5" * "2" is 10, then 10 + "5" concatenates to "105", then "105" - 2 converts to a number: 103.
Write an expression that tells you whether an integer n is odd, correct for negative numbers too.
Answer
n % 2 !== 0. Testing n % 2 === 1 fails for negatives because -3 % 2 is -1.
What are i and x after this code?
let i = 3;
const x = i++ * 2 + --i;
Answer
i is 3 and x is 9. i++ gives 3 (i becomes 4), so 3 * 2 is 6; --i makes i 3 and gives 3; 6 + 3 is 9.
A carousel has 5 slides and current is the index. How do you go back one slide, wrapping from 0 to 4?
Answer
current = (current - 1 + 5) % 5; or, more generally, current = mod(current - 1, slides.length) with the mod helper from this article. Plain (current - 1) % 5 gives -1 when current is 0.
Why does this code return "1013"?
[10, 1, 3].reduce((sum, x) => sum + x, "")
Answer
The starting value is the empty string, so the first + concatenates and every later step joins strings. Start with 0 to add numbers.
Previous: JavaScript Operator Precedence, Associativity and Evaluation Order
Next: JavaScript Bitwise Operators: AND, OR, XOR, NOT, Shifts and Bit Flags

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.