Assignment Operators
Assignment stores a value in a writable target. Compound assignments combine an operation with storing its result.
reads += 1 means calculate reads + 1, then put the result back.
See the mechanism
2Follow one example
After +=, reads is 5. Postfix reads++ returns the old value 5 to previous and also increments reads to 6. Prefix ++reads increments first and returns the new value. Prefer a separate update when combining mutation and a larger expression would obscure the order.
Predict, change and check
Predict: What would previous contain if you used ++reads instead of reads++?
Test your explanation: Try the prefix form, then rewrite the update as two separate statements that make the old and new values explicit.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
Assignment is an effect with a resulting value
Compound assignment updates its target after evaluating the operation. += can concatenate strings, so it is not always numeric addition. Destructuring can assign several values, but it does not recursively clone referenced objects. A const binding cannot be reassigned through a compound operator either.
Logical assignment ||=, &&= and ??= assigns conditionally under different rules. ??= preserves valid zero, false and empty text while filling only nullish values. Choose the intended defaulting policy and test that an already supplied value does not get overwritten.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.