3.3 The Assignment Operators

The second category of operators is the assignment family. You've already used the simple assignment operator = to store a value in a variable, for example x = 10. JavaScript also offers compound assignment operators that combine assignment with arithmetic. They are shortcuts that read the current value of a variable, transform it, and assign the new value back in a single statement.

From increment to compound assignment

The increment operator x++ seen in the previous lesson is equivalent to x = x + 1. But what if you want to add 5 instead of 1? The increment operator won't help. The compound operator += does:

x = x + 5;  // explicit form
x += 5;     // shortcut — exactly equivalent

The same logic applies to multiplication: x = x * 3 can be written x *= 3. Every arithmetic operator has its compound assignment variant:

  • += — add then assign
  • -= — subtract then assign
  • *= — multiply then assign
  • /= — divide then assign
  • %= — remainder then assign
  • **= — exponentiate then assign

Compound operators keep code concise and intent-revealing. In the next video, we'll move on to comparison operators.