SyntaxStudy
Sign Up
JavaScript Logical Assignment Operators
JavaScript Intermediate 3 min read

Logical Assignment Operators

Logical Assignment

ES2021 added &&=, ||=, and ??= for concise conditional assignment patterns.

Example
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;
Pro Tip

??= is ideal for initialising config objects: options.timeout ??= 5000.