Skip to the workspace
Pebellslearn by building
Expressions & Operators  ›  Lesson

Assignment Operators

Assignment Operators 12 min

javascript / Assignment Operators

Explore each scene at your own pace.

01 · Assignment Operators

Assignment Operators

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

Walk through the modelAssignment OperatorsAuthored snapshots of the example; use Run below to test the code.
reads2
A let binding can receive an updated value.
1 of 3

Follow one example

javascript

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.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.