This is a stage-4 proposal feature will be included in ES2021, supports logical assignment with the new operators
a ||= b a &&= b a ??= b
=): 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.
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
undefined, nullish values are always falsy.
New logical assignment operators
??= 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