Arithmetic & Assignment Operators

Operators are the symbols that combine values: + to add, = to assign, ++ to count up. This lesson covers the arithmetic and assignment operators, the order in which they're applied, and the one operator that behaves surprisingly — + with strings.

Arithmetic operators

JS arithmetic.js
console.log(7 + 2);
console.log(7 - 2);
console.log(7 * 2);
console.log(7 / 2);
console.log(7 % 2);
console.log(7 ** 2);
Output
9
5
14
3.5
1
49
OperatorNameNotes
+ - * /Add, subtract, multiply, divideDivision always gives a decimal when needed — there's no separate integer division
%RemainderWhat's left over: 7 % 2 is 1. Handy for "every nth item" and even/odd checks
**Exponent2 ** 10 is 1024

Dividing by zero doesn't crash — it produces Infinity (or -Infinity). Doing maths with something that isn't a number produces NaN, "Not a Number". Lesson 16 covers both.

Operator precedence

As in school maths, multiplication and division happen before addition and subtraction, and parentheses override everything:

JS precedence.js
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(2 ** 3 ** 2);
console.log(10 - 4 - 3);
Output
14
20
512
3

Most operators work left to right (10 - 4 - 3 is (10 - 4) - 3), but ** works right to left, so 2 ** 3 ** 2 is 2 ** 9. When in doubt, add parentheses — they cost nothing and make intent clear.

Assignment operators

= assigns a value. Compound operators update a variable using its current value:

JS assignment.js
let points = 10;

points += 5;
console.log(points);
points -= 3;
console.log(points);
points *= 2;
console.log(points);
points /= 4;
console.log(points);
points %= 4;
console.log(points);
points **= 3;
console.log(points);
Output
15
12
24
6
2
8

points += 5 is shorthand for points = points + 5. There are also logical assignment operators — ||=, &&= and ??= — covered in the next lesson.

Increment and decrement

++ adds one and -- subtracts one. Where you put them matters when you also use the result:

JS increment.js
let count = 5;

console.log(count++);
console.log(count);
console.log(++count);
console.log(--count);
Output
5
6
7
6

count++ (postfix) returns the old value, then increments. ++count (prefix) increments first, then returns the new value. To avoid confusion, many developers only use ++ as a statement on its own line, or write count += 1.

+ with strings

If either side of + is a string, JavaScript joins (concatenates) instead of adding:

JS concat.js
console.log("Hello, " + "world");
console.log("Total: " + 5 + 5);
console.log("Total: " + (5 + 5));
console.log(5 + 5 + " items");
console.log("5" * 2);
console.log("5" - 2);
Output
Hello, world
Total: 55
Total: 10
10 items
10
3

Reading left to right: "Total: " + 5 becomes the string "Total: 5", and then + 5 joins another 5. Parentheses fix it. The other arithmetic operators don't concatenate, so they convert strings to numbers — "5" * 2 is 10. This automatic conversion is called type coercion; lesson 18 explains the rules and how to avoid surprises.

Common bug: values from form fields are always strings. input.value + 1 with an input of "4" gives "41", not 5. Convert first with Number(input.value).

Unary plus and minus

A + or - in front of a single value converts it to a number (and - negates it): +"42" is 42. It's compact, but Number("42") is clearer to read.

Try it yourself

Try it yourself
Console output