Learn JavaScript
❯ operators.op_arithmetic

Arithmetic Operators in JavaScript

$concept

JavaScript arithmetic (+ - * / % **) coerces operands toward numbers — "5" * 2 is 10, while "5" + 2 is "52" because + prefers strings when either side is one. Division by zero yields Infinity (not an error), 0/0 yields NaN, and % keeps the dividend's sign — -7 % 3 is -1.

The ** exponent operator (2 ** 10 === 1024) replaced Math.pow in modern code, and ++/-- step by one with prefix/postfix timing identical to C. Mixed-type + is the #1 coercion surprise: numbers add, strings concatenate, and mixed means concatenate.

$how it works
1

+ concatenates with strings

"5" + 2 is "52" (string wins); "5" - 2 is 3 (only + prefers strings — every other operator coerces to number).

2

Division never throws

1/0 is Infinity, -1/0 is -Infinity, 0/0 is NaN — check isFinite/isNaN instead of catching.

3

** exponentiates

2 ** 10 === 1024, right-associative (2 ** 3 ** 2 is 2^9) — modern replacement for Math.pow.

4

++ / -- timing

++i increments before use, i++ after — identical C semantics for counters and indexes.

$example
console.log(7 / 2, 7 % 2);
console.log("5" + 2, "5" - 2);
console.log(2 ** 10);
Output
3.5 1
52 3
1024
17 / 2 is true division (3.5 — no integer truncation like C); 7 % 2 is remainder 1.
2+ concatenates to "52" while - coerces to 3 — the asymmetry to memorize.
3Exponentiation gives 1024 in one operator.
$use cases
Remembering + is the only concatenating operator (all others coerce numeric)
Handling Infinity/NaN results instead of expecting thrown errors
Using ** for powers in modern codebases
Modulo for wrapping, parity, and digit work

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.