Sub topic 16
Compound and Logical Assignment
This chapter assumes you already know how ||, &&, and ?? short-circuit, and how + decides between addition and concatenation. Both topics come up again here, because assignment operators reuse those exact rules.
Compound Assignment
A compound assignment operator merges a binary operator with =. Writing x += 2 gives the same result as x = x + 2. JavaScript has one for almost every binary operator: +=, -=, *=, /=, %=, **=, <<=, >>=, >>>=, &=, |=, and ^=.
let count = 5;
count += 2; // 7
count **= 2; // 49
let label = 'item';
label += 1; // 'item1': += follows the same concatenation rules as +
console.log(count, label);The longhand version reads the left-hand side twice. The compound version evaluates the target reference only once, which changes the result when the target expression has side effects:
const arr = [10, 20];
let i = 0;
arr[i++] += 5; // i++ runs once
console.log(arr, i); // [ 15, 20 ] 1If you wrote arr[i++] = arr[i++] + 5, the increment would run twice and the code would read and write different slots.
Logical Assignment
The three logical assignment operators are ||=, &&=, and ??=. Each one assigns only when its short-circuit condition lets the right side run:
| Operator | Assigns when the current value is | Equivalent to | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `a \ | \ | = b` | falsy | `a \ | \ | (a = b)` |
| `a &&= b` | truthy | `a && (a = b)` | ||||
| `a ??= b` | `null` or `undefined` | `a ?? (a = b)` |
Look at the "Equivalent to" column. The form is a || (a = b), not a = a || b. When the condition fails, no write happens at all.
const opts = { retries: 0, name: '' };
opts.retries ||= 3; // retries becomes 3 because 0 is falsy
opts.name ??= 'anon'; // name stays '' because '' is not nullish
console.log(opts); // { retries: 3, name: '' }The retries line is the same falsy-value bug that || causes for defaults. If 0 is a legitimate value, use ??= instead.
Why "no write" matters
Skipping the write is something you can observe. A setter never fires, so neither do any change listeners, proxies, or framework reactivity hooks behind it:
let writes = 0;
const obj = {
_v: 'set',
get v() { return this._v; },
set v(x) { writes++; this._v = x; },
};
obj.v ||= 'fallback'; // truthy, so no write
obj.v = obj.v || 'fallback'; // always writes
console.log(writes); // 1The right-hand side is skipped as well. In cache[key] ??= compute(key), compute runs only on a cache miss, which makes this pattern a short way to memoize.
Exercise
After running let a = null; let b = 0; let c = 'x'; a ??= 1; b ||= 2; c &&= c.toUpperCase();, what are a, b, and c?
Show answer
a is 1 because null is nullish. b is 2 because 0 is falsy. c is 'X' because 'x' is truthy, so the right side runs and its result is assigned.
Exercise
An object has a setter config.level that logs every write, and config.level is currently 0. How many log lines do config.level ??= 5 and config.level ||= 5 produce, run in that order?
Show answer
One. ??= sees 0, which is not nullish, so it does not write. ||= sees 0, which is falsy, so it writes 5 and the setter logs once.