Arithmetic
Arithmetic operators are the symbols JavaScript uses to do maths: addition, subtraction, multiplication, division, remainder and powers. You use them for everything from totalling a shopping cart to calculating a percentage or working out whether a number is even.
The basic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division | 7 / 2 | 3.5 |
% | Remainder | 7 % 3 | 1 |
** | Exponent | 2 ** 5 | 32 |
Division always gives a decimal result if needed; there is no separate integer division. Use Math.floor(7 / 2) or Math.trunc when you want a whole number.
Remainder and increments in practice
The % operator returns what is left over after division. It is perfect for checking even and odd numbers (n % 2 === 0) or for wrapping an index back to the start of a list. Note that with negative numbers the result keeps the sign of the left side, so -7 % 3 is -1, not 2.
The increment ++ and decrement -- operators add or subtract one. Their position matters when the value is used in the same expression: count++ returns the old value and then increases it, while ++count increases first and returns the new value. Compound assignment operators such as +=, -=, *= and **= update a variable in place.
let price = 499;
let qty = 3;
let subtotal = price * qty; // 1497
let discount = subtotal * 0.1; // 149.7
let total = subtotal - discount; // 1347.3
let items = 0;
items += qty; // 3
console.log(items++); // prints 3, items is now 4
console.log(++items); // prints 5
console.log(10 % 4); // 2
console.log(2 ** 10); // 1024
The + operator and strings
The plus sign has two jobs. With numbers it adds; if either side is a string, it joins text instead. That causes the most famous arithmetic bug in JavaScript: values read from an input box or a URL are strings, so "5" + 3 gives "53", not 8.
The other operators do not join text. They try to convert both sides to numbers, so "5" - 3 is 2 and "5" * "2" is 10. That inconsistency is why you should convert explicitly with Number(value) before calculating, rather than relying on automatic conversion.
Special results: NaN, Infinity and decimals
- A calculation that makes no sense, like
"abc" * 2, producesNaN("not a number"). Any further maths withNaNalso givesNaN. Check withNumber.isNaN(x). - Dividing by zero does not throw an error.
5 / 0isInfinity, and0 / 0isNaN. - Numbers are stored in binary floating point, so some decimals are not exact:
0.1 + 0.2is0.30000000000000004. For money, calculate in the smallest unit (paise) as whole numbers, and format the result withtoFixed(2)only for display.
Order of operations
JavaScript follows the usual maths order: ** first, then *, / and %, then + and -, working left to right within the same level. So 2 + 3 * 4 is 14. Use parentheses whenever the intent might be unclear; (2 + 3) * 4 is 20 and reads clearly to everyone.
Points to keep: convert input to numbers before adding, watch for NaN, remember that decimals can be slightly inexact, and use brackets to make the order obvious.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.