Module 3: Operators and User Interaction
Compound Assignments
Compound assignment operators are shorthand for performing an operation and then assigning the result back to the original variable.
+=
(Addition assignment):x += y
is equivalent tox = x + y
-=
(Subtraction assignment):x -= y
is equivalent tox = x - y
*=
(Multiplication assignment):x *= y
is equivalent tox = x * y
/=
(Division assignment):x /= y
is equivalent tox = x / y
%=
(Modulus assignment):x %= y
is equivalent tox = x % y
**=
(Exponentiation assignment):x **= y
is equivalent tox = x ** y
let score = 100; score += 50; // score is now 150 let price = 20; price *= 0.9; // price is now 18 (10% discount) let counter = 5; counter %= 3; // counter is now 2 (remainder of 5/3)
These operators make code more concise and often easier to read.