Arithmetic Operators in JavaScript
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.
+ concatenates with strings
"5" + 2 is "52" (string wins); "5" - 2 is 3 (only + prefers strings — every other operator coerces to number).
Division never throws
1/0 is Infinity, -1/0 is -Infinity, 0/0 is NaN — check isFinite/isNaN instead of catching.
** exponentiates
2 ** 10 === 1024, right-associative (2 ** 3 ** 2 is 2^9) — modern replacement for Math.pow.
++ / -- timing
++i increments before use, i++ after — identical C semantics for counters and indexes.
console.log(7 / 2, 7 % 2);console.log("5" + 2, "5" - 2);console.log(2 ** 10);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.