Assignment and Compound Assignment Operators
Update a count when students arrive and leave
A class has ten students present. Five arrive late and two leave for another activity. A program must update the current count at each event.
Assignment associates a value with a name. Compound assignment combines a calculation with storing its result; for a simple numeric variable, count += 5 means count = count + 5.
Trace updates
Use the current value as input to each later assignment.
Choose readable syntax
Compare expanded assignment with +=, -= and increments.
Avoid type mistakes
Explain why += can join text instead of increasing a numeric count.
The current count changes after each event
Initial count
Store the starting attendance.
10Arrivals
Add five to the current ten.
10 + 5 → 15Departures
Subtract two from the updated fifteen.
15 - 2 → 13
An update uses the old value before storing the new one
Read the current value, calculate, then assign. The second update uses 15 rather than the original ten. These statements are actions performed in order; they are not permanent mathematical equations.
JavaScript offers +=, -=, *=, /=, %= and **=, among other forms. Choose the operator that states your update. Use let when reassigning a name; const prevents reassignment of that name.
Before starting, review assignment and current stored values. These examples use JavaScript; other languages can use different syntax and rules.
Follow arrivals and departures
Each console output records a stage in the attendance update. Starting at ten, += 5 stores fifteen, and -= 2 then stores thirteen.
Expanded assignments would produce the same result for this simple variable. The shorthand saves repetition without changing the intended numeric rule.
JavaScript · local teaching example
let presentCount = 10;
console.log(presentCount);
presentCount += 5;
console.log(presentCount);
presentCount -= 2;
console.log(presentCount);Expected output
10
15
13Standalone increments are easier to read
JavaScript ++ increases a numeric variable by one. The postfix form returns the old value when used inside a larger expression, while the prefix form returns the updated value. Both change the stored count.
This example prints ten from the postfix expression, then eleven from the stored value, then twelve from the prefix expression. Prefer standalone updates when the distinction would obscure the task.
JavaScript · local teaching example
let count = 10;
console.log(count++);
console.log(count);
console.log(++count);Expected output
10
11
12The same += syntax can join text
The initial value here is a string. JavaScript += applies the + operation and stores its result, so the count becomes the string “105.” It has not counted five additional students.
Know the type before updating. A numeric count should start numeric or be converted and validated explicitly.
JavaScript · local teaching example
let count = '10';
count += 5;
console.log(count);
console.log(typeof count);Expected output
105
stringApply the rule to a small change
Start at ten, add five arrivals, subtract two departures, then add one returning student. Display the final count.
Worked answer
The final count is fourteen. You may write count += 1 or a standalone count++; avoid embedding an update in unrelated output or comparisons when it makes the order harder to follow.
JavaScript · local teaching example
let count = 10;
count += 5;
count -= 2;
count += 1;
console.log(count);Expected output
14Recognize mistakes and limits
Assigning a negative count can be valid JavaScript but invalid attendance. Guard task-specific limits separately from the update syntax. Assignment is not the equality operator ===.
For a simple variable, x += y expresses an addition update. With more complex property expressions, the left-hand target is evaluated once; blindly expanding it can repeat calls or index updates. Keep beginner updates explicit and free of surprising side effects.
For the language or tool rules used here, see JavaScript operator rules.
Check the core idea
JavaScript · code for this question
let presentCount = 10;
console.log(presentCount);
presentCount += 5;
console.log(presentCount);
presentCount -= 2;
console.log(presentCount);Apply the idea to a new situation
JavaScript · code for this question
let count = '10';
count += 5;
console.log(count);
console.log(typeof count);Update one meaningful value at a time
Compound assignment calculates from the current value and stores the result. Trace updates in execution order, know the stored type, and keep increment side effects visible.
Connect this idea with updating values during repetition and the operations inside updates.
Reaching the recap and completing the quizzes are separate milestones.