JavaScript Compound Assignment and Update Operators
Quick answer
Compound assignment updates an existing writable location using an operation such as addition. Increment and decrement change a numeric value by one; their prefix and postfix forms differ in the value produced by the expression.
How it works
total += fee reads total, adds fee, and writes back. Other arithmetic compounds include -=, *=, /=, %=, and **=; each applies its corresponding operator. For a simple numeric binding this behaves like total = total + fee; a complex assignment target is evaluated only once by compound assignment. ++n returns the updated numeric value, whereas n++ returns the previous numeric value after conversion. The target must be assignable. Number and BigInt operands cannot be mixed in arithmetic; string += can concatenate.
let points = 8;points += 5;points -= 2;console.log(points);Expected output in Node.js:
11The two assignments update the same binding in order.
let count = 3;console.log(count++);console.log(++count);console.log(count);Expected output in Node.js:
355Postfix yields 3 and stores 4; prefix stores and yields 5.
Common mistakes
Do not confuse the value of an update expression with the final stored value. Avoid embedding multiple updates in one expression when separate statements communicate intent more clearly.
Practice
Practice updating a starting balance by a credit and debit, then return the final balance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Compound Assignment and Update Operators — Knowledge Check
Apply the taught concept and reason about the result.
After let n=5; n+=3; n*=2; what is n?
What does let n=4; console.log(n++); print?
What does let n=4; console.log(--n); print?
What is the value after let x="3"; x+=2; ?
How many times is the assignment target evaluated by a compound assignment?