TExcelTAD
Home › JavaScript › JS Arithmetic

JavaScript Arithmetic

JavaScript has one number type for both whole numbers and decimals, and a familiar set of arithmetic operators to work with them.

Operator Name Example Result
+ Addition 8 + 3 11
- Subtraction 8 - 3 5
* Multiplication 8 * 3 24
/ Division 8 / 3 2.6666666666666665
% Remainder 8 % 3 2
** Exponentiation 8 ** 3 512
++ Increment x++ adds 1
-- Decrement x-- subtracts 1
js · try it yourself
const a = 17;
const b = 5;
console.log(a + b, a - b, a * b);
console.log(a / b);
console.log(a % b);
console.log(b ** 2);
22 12 85
3.4
2
25

Division and remainder

/ always gives the exact (decimal) result — there's no separate integer division. To get the whole-number part, wrap it in Math.floor() or Math.trunc(). The remainder operator % gives what's left over, which is perfect for "every n-th" and "is it even?" checks:

js · try it yourself
const minutes = 135;
const hours = Math.floor(minutes / 60);
const rest = minutes % 60;
console.log(`${hours}h ${rest}min`);

console.log(10 % 2 === 0 ? "10 is even" : "10 is odd");
console.log(7 % 2 === 0 ? "7 is even" : "7 is odd");
2h 15min
10 is even
7 is odd

Increment and decrement

++ and -- add or subtract 1. Placed after the variable (x++), the expression gives the old value; placed before (++x), it gives the new one:

js · try it yourself
let count = 5;
console.log(count++);  // prints 5, then count becomes 6
console.log(count);
console.log(++count);  // count becomes 7, then prints 7
console.log(count--);  // prints 7, then count becomes 6
console.log(count);
5
6
7
7
6

Tip: On a line of its own, count++ and ++count do the same thing. The difference only matters when the result is used inside a larger expression — and such code is hard to read, so keep ++ on its own line.

Precedence

Operators run in a fixed priority order: ** first, then *, /, %, then + and -. Operators of the same level run left to right (except **, which runs right to left). Parentheses override everything:

js · try it yourself
console.log(2 + 3 * 4);       // 3 * 4 first
console.log((2 + 3) * 4);
console.log(20 / 4 * 2);      // left to right: (20 / 4) * 2
console.log(2 ** 3 ** 2);     // right to left: 2 ** 9
console.log(-(2 ** 2));
14
20
10
512
-4

Floating-point surprises

Numbers are stored in binary floating point (IEEE 754), which can't represent some decimals exactly. The result is tiny rounding errors:

js · try it yourself
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log((0.1 + 0.2).toFixed(2));
// For money, calculate in whole cents:
console.log((10 + 20) / 100);
0.30000000000000004
false
0.30
0.3

Special results

Some operations produce special number values instead of an error:

Expression Result Meaning
5 / 0 Infinity larger than any number
-5 / 0 -Infinity
0 / 0 NaN "Not a Number" — an invalid result
"abc" * 2 NaN the string can't be converted
"6" * 2 12 *, /, - convert strings to numbers

Test yourself

What is 23 % 5?

  1. 4.6
  2. 4
  3. 3
  4. 5

With let n = 3;, what does console.log(n++) print?

  1. 3
  2. 4
  3. 2
  4. undefined

What is (2 + 3) ** 2?

  1. 11
  2. 25
  3. 10
  4. 13