Logical Assignment

This is a stage-4 proposal feature will be included in ES2021, supports logical assignment with the new operators &&=, ||=, and ??=.

a ||= b
a &&= b
a ??= b

Related Concepts

Assignment (=): Used to assign a value to a variable

Logical operators (&&, ||, ??): Typically used with boolean (logical) values, and when they are, they return a boolean value.

Short-circuit evaluation: The semantics of some logical operators in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.

Falsy: JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops. Following are falsy values: false, 0, 0n, "", null, undefined, NaN.

Truthy: A truthy value is a value that is considered true when encountered in a Boolean context, all values are truthy unless they are defined as falsy.

Nullish: A nullish value is the value which is either null or undefined, nullish values are always falsy.

Logical Assignment Operators

New logical assignment operators &&=, ||=, and ??= are unlike their mathematical and bitwise counterparts, logical assignments follow the short-circuiting behavior of their respective logical operations. They only perform an assignment if the logical operation would evaluate the right-hand side.

a ||= b
// Logical OR assignment
// Equivalent to: a || (a = b);
// Only assigns if a is falsy

a &&= b
// Logical AND assignment
// Equivalent to: a && (a = b);
// Only assigns if a is truthy

a ??= b
// Logical nullish assignment
// Equivalent to: a ?? (a = b);
// Only assigns if a is nullish

More info: TC39 proposal, V8, MDN