Logical assignment (&&=, ||=, ??=)
Combines a logical operation with assignment, assigning conditionally.
Syntax
a ||= b
a &&= b
a ??= b
Returns
any — The final value of the left-hand reference after the conditional
assignment.
Examples
let a = 0;
a ||= 5;
console.log(a);
Output
5
let b = 1;
b &&= 10;
console.log(b);
Output
10
const opts = { timeout: 0 };
opts.timeout ??= 1000;
console.log(opts.timeout);
Output
0
Notes
- These short-circuit: the assignment only happens (and `b` is only
evaluated) when the condition warrants it.
- `??=` assigns only when the target is `null` or `undefined`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 85 |
| firefox | 79 |
| safari | 14 |
| edge | 85 |
| node | 15.0 |