Canddi

What are compound operators in JavaScript?

Compound assignment operators update a value using a shorter expression. For example, points += 10 adds 10 to the current value of points and stores the result back in that variable.

How compound assignment works

let points = 5;
points += 10;
console.log(points); // 15

For this simple variable, the result is the same as writing points = points + 10. With a more complex left-hand expression, there is an important distinction: compound assignment evaluates that expression once. See MDN’s addition-assignment reference.

Use let for variables you intend to reassign. An arithmetic assignment such as points += 10 cannot reassign a const binding.

Six arithmetic assignment operators

JavaScript has more than five compound assignment operators. These six cover common arithmetic updates.

Operator Operation
+= Add
-= Subtract
*= Multiply
/= Divide
%= Remainder
**= Raise to a power

The examples below use a separate variable for each calculation. In particular, 4 multiplied by 9 is 36.

let count = 10;
count -= 10;
console.log(count); // 0

let size = 4;
size *= 9;
console.log(size); // 36

let total = 50;
total /= 25;
console.log(total); // 2

let remainder = 50;
remainder %= 9;
console.log(remainder); // 5

let power = 3;
power **= 2;
console.log(power); // 9

% is the remainder operator. Here, 50 divided by 9 leaves a remainder of 5. With negative operands, remainder and mathematical modulo can differ.

Why += sometimes joins text

+= can concatenate strings as well as add numbers. If a number is combined with a string in these examples, the result is text.

let label = "Lesson";
label += " complete";
console.log(label);
// "Lesson complete"

let amount = 5;
amount += "10";
console.log(amount); // "510"

If you intended numerical addition, check and convert your input to a suitable number before calculating. Do not assume that a value which looks numeric is already a number.

Logical and bitwise assignment

Logical assignment is conditional: &&= assigns when the existing value is truthy, ||= when it is falsy, and ??= when it is null or undefined. The right-hand expression is evaluated only when that condition is met.

Zero is falsy, but it is not nullish. That makes these two updates behave differently:

let first = 0;
first ||= 12;
console.log(first); // 12

let second = 0;
second ??= 12;
console.log(second); // 0

Read MDN’s logical OR assignment guide before using this shorthand for defaults.

Bitwise assignment operators also exist, including &=, |=, ^=, <<=, >>= and >>>=. They operate on bit representations; consult the full operator reference when you need them.

Assignment, comparison and readability

= assigns a value. === compares values using strict equality and does not assign anything. Choose the operator according to what the statement should do.

Compound assignment can make an update easier to read by avoiding repetition. Fewer characters do not establish a performance improvement. Prefer clear code, and measure any performance concern in the application where it matters.

Try a short exercise

Read these statements in order and predict the final value before running the code. Each update uses the result of the previous one.

let credits = 6;
credits += 4;
credits *= 3;
credits %= 7;
console.log(credits); // 2

The steps produce 10, then 30, then a remainder of 2. Change one operand and predict the result again. This is a useful way to practise the difference between an operation and the value stored afterwards.

Build This Skill

For guided practice with variables, expressions, operators and program flow, explore Academy Class’s JavaScript Beginner course. Review its current outline and entry guidance to decide whether it fits your starting point.