What an augmented assignment is
Changing a variable based on its own value is a very common step. Since the right side of = is evaluated first, this line adds 5 to myVar:
myVar = myVar + 5;
An augmented assignment (also called a compound assignment) does the arithmetic and the assignment in one operator, so the variable name is written only once:
let myVar = 10;
myVar += 5;
console.log(myVar); // 15
There is one such operator for each arithmetic operator:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+= | x = x + y | 10 then x += 5 | 15 |
-= | x = x - y | 10 then x -= 5 | 5 |
*= | x = x * y | 3 then x *= 5 | 15 |
/= | x = x / y | 20 then x /= 4 | 5 |
%= | x = x % y | 11 then x %= 3 | 2 |
**= | x = x ** y | 3 then x **= 3 | 27 |
The variable must be declared with let. A const variable can't be reassigned, so TypeScript reports an error on any of these operators, and the JavaScript it writes stops with a TypeError when it reaches the assignment.
The right side is computed first
With an augmented assignment, the whole right side is evaluated before the operator is applied. That matters when the right side is more than a single value:
let total = 100;
total -= 3 * 2; // total = total - (3 * 2)
console.log(total); // 94
total *= 1 + 1; // total = total * (1 + 1), not (total * 1) + 1
console.log(total); // 188
An augmented assignment is also an expression, and its value is the new value of the variable. After let x = 4; let y = (x += 3);, both x and y are 7. You will rarely need this, but it explains why the operators can appear inside larger expressions.
Increment and decrement
Adding or subtracting exactly 1 is so frequent that it has its own operators. x++ does the same as x += 1, and x-- does the same as x -= 1. As separate statements, the prefix forms ++x and --x behave the same way. They differ only when the expression's value is used.
The exercise, step by step
The lesson walks one variable, number, through every operator and tracks its value. The values below come from running the steps.
- Declare
numberwith the typenumberand the value18. Uselet, since every later step reassigns it. number += 8adds 8, giving26.number--subtracts 1, giving25.number /= 5divides by 5, giving5. In TypeScript every number is floating-point, so25 / 5is just5. Had the division not been even, the result would keep its decimals:7 / 2is3.5.number++adds 1, giving6.number *= 3multiplies by 3, giving18.number -= 18subtracts 18, giving0. The value returns to zero because the previous step left it at 18.
The solution in TypeScript
let number: number = 18;
number += 8;
number--;
number /= 5;
number++;
number *= 3;
number -= 18;
A few details:
- The annotation is not enforced at run time.
: numberis checked by the compiler and then erased from the JavaScript output. - The name
numberis allowed. It is also the name of a type, but types and variables live in separate namespaces, so a variable can have that name. - The order matters. Each step uses the value left by the one before. Moving
number *= 3earlier would change every later value.
Why use them
The short form is not only less typing. It makes the intent obvious: score += 10 reads as "raise score by 10", while score = score + 10 makes the reader check that both names are the same. With long names such as currentPlayerScore, that check is easy to get wrong when typing and easy to miss when reading.
The same idea exists for a few other operators. &&=, ||= and ??= assign only in some cases: x ??= 5 stores 5 only if x is null or undefined. This lesson focuses on the arithmetic ones.
Gotchas
+=with strings. It concatenates:let s = "ab"; s += "cd";makessequal to"abcd". If the variable is typednumber, the compiler reports an error for adding a string to it, although the JavaScript it still writes would concatenate (1plus"a"gives"1a").- Order of the operator characters. The operator is
+=, not=+. The textx =+ 5is parsed asx = +5, which setsxto 5 instead of adding. - Division is not integer division.
/=keeps the fractional part. To keep only the whole part, round afterwards:x = Math.floor(x). - Left side evaluated once. In
a[i++] += 2, the index expression runs once, which is a small advantage over writinga[i++] = a[i++] + 2.
Key takeaways
x op= yis shorthand forx = x op y.- The operators are
+=,-=,*=,/=,%=and**=. ++and--change a variable by exactly 1.- The variable must be declared with
let.