Simple assignment is done with the equal sign (=).
It assigns the value on the right to the variable on the left:
let num = 10;
Compound assignment is done with one of the multiplicative, additive, or bitwise-shift operators followed by an equal sign (=).
These assignments are designed as shorthand for such common situations as this:
let num = 10;
num = num + 10;
The second line of code can be replaced with a compound assignment:
let num = 10;
num += 10;
Compound-assignment operators exist for each of the major mathematical operations and a few others as well.
Description | Operator |
---|---|
Multiply/assign | *= |
Divide/assign | /= |
Modulus/assign | %= |
Add/assign | += |
Subtract/assign | -= |
Left shift/assign | <<= |
Signed right shift/assign | >>= |
Unsigned right shift/assign | >>>= |