Sub topic 9
Filling In Missing Values with ??=
This chapter assumes you already know how ?? chooses a fallback and how || treats every falsy value as missing. Here you will learn the assignment form, ??=, which writes a default into a variable or property only when the current value is null or undefined.
The Basic Pattern
The nullish coalescing assignment operator (??=) checks the left side. If that value is nullish, it assigns the right side. If not, it leaves the value alone.
const options = { retries: 0 };
options.retries ??= 3; // stays 0
options.timeout ??= 1000; // set to 1000
console.log(options); // { retries: 0, timeout: 1000 }retries keeps its value of 0 because 0 is not nullish. timeout did not exist, so reading it gave undefined, and the default was written.
Not Quite `a = a ?? b`
A common mental model is that a ??= b means a = a ?? b. The specification actually defines it as a ?? (a = b). The difference matters because the long form always performs an assignment, even when the value doesn't change.
You can see the difference when the target is a setter:
const config = {
_mode: "dark",
writes: 0,
get mode() { return this._mode; },
set mode(value) { this.writes++; this._mode = value; }
};
config.mode ??= "light";
console.log(config.writes); // 0 (no assignment happened)
config.mode = config.mode ?? "light";
console.log(config.writes); // 1 (setter ran, writing "dark" again)Skipped assignments also matter for reactive state, proxies, and frozen objects. With ??=, nothing is written unless something is actually missing.
The Right Side Is Evaluated Lazily
The right-hand expression only runs when an assignment is needed. That makes ??= a natural fit for caching:
const cache = {};
function load(key) {
console.log("loading", key);
return key.toUpperCase();
}
cache.a ??= load("a"); // logs "loading a"
cache.a ??= load("a"); // logs nothing; cached value reused
console.log(cache.a); // "A"Comparing the Logical Assignment Operators
??= shipped in ES2021 alongside ||= and &&=. All three short-circuit in the same way. They differ only in the condition that triggers assignment.
| Operator | Assigns when the left side is | `0 OP 5` result | `"" OP "x"` result | Column | Column |
|---|---|---|---|---|---|
| `a | = b` | falsy | `5` | `"x"` | |
| `a &&= b` | truthy | `0` | `""` | ||
| `a ??= b` | `null` or `undefined` | `0` | `""` |
Use ??= for defaults when 0, "", or false are valid settings. Use ||= only when every falsy value really should be replaced.
Common Mistakes
Mutating a caller's object. Applying ??= directly to a parameter changes the object the caller passed in. Copy it first:
function connect(userOptions = {}) {
const opts = { ...userOptions };
opts.port ??= 8080;
opts.secure ??= true;
return opts;
}
const input = { secure: false };
console.log(connect(input)); // { secure: false, port: 8080 }
console.log(input); // { secure: false } (unchanged)Using an undeclared variable. missingVar ??= 1 throws a ReferenceError. The operator checks the value, not whether the variable exists, so declare the variable first with let.
Pairing with optional chaining on the left. user?.settings ??= {} is a SyntaxError, because an optional chain cannot be an assignment target. A later chapter covers this rule.
Exercise
Given const s = { volume: 0, name: null, muted: false };, what does s contain after running s.volume ??= 50; s.name ??= "Guest"; s.muted ||= true; s.theme ??= "light";?
Show answer
{ volume: 0, name: "Guest", muted: true, theme: "light" }. volume stays 0 because 0 is not nullish. name was null, so it becomes "Guest". muted changes to true because ||= replaces the falsy value false. theme was undefined, so it is added.
Exercise
A counter object has a setter on count that logs every write, and count currently returns 7. How many times does the setter run for obj.count ??= 0 compared with obj.count = obj.count ?? 0?
Show answer
Zero times for obj.count ??= 0, because 7 is not nullish and the assignment is skipped. One time for obj.count = obj.count ?? 0, because that form always assigns, even though it writes 7 back.