CKTIS / JavaScript
Nullish assignment: ??=
The ??= operator assigns only when the target contains null or undefined. It preserves 0, false, and the empty string.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
target ??= expressiontarget ??= expression reads the target and assigns the result of expression only for null or undefined. It returns the retained or assigned value.
The left side is evaluated once. When assignment is skipped, the right side is not evaluated and no setter is called. Reading can itself invoke a getter.
Unlike ||=, it preserves 0, false, "", and NaN. Plain ?? does not assign. The target must be a valid assignment target; the operator does not declare a variable.
The operator was introduced in ECMAScript 2021 (12th edition).
Examples
const settings = { retries: 0, label: null };
let calls = 0;
function fallback() {
calls += 1;
return "Guest";
}
settings.retries ??= fallback();
settings.label ??= fallback();
console.log(JSON.stringify(settings));
console.log(calls);Result
{"retries":0,"label":"Guest"}
1The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.