Logical Assignment
ES2021 added &&=, ||=, and ??= for concise conditional assignment patterns.
ES2021 added &&=, ||=, and ??= for concise conditional assignment patterns.
let a = null;
a ??= "default"; // a = "default" (only if null/undefined)
let b = 0;
b ||= 42; // b = 42 (assigns if falsy)
let c = 1;
c &&= c * 2; // c = 2 (assigns if truthy)
// Equivalent verbose forms:
a = a ?? "default";
b = b || 42;
c = c && c * 2;
??= is ideal for initialising config objects: options.timeout ??= 5000.
More in JavaScript