The logical OR assignment (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
||=) operator evaluates the left operand and assigns the right operand to the left operand strictly if the left operand is falsy. It leverages short-circuit evaluation to prevent both unnecessary assignments and the evaluation of the right operand when the left operand is truthy.
Logical Equivalence
The operationx ||= y is logically equivalent to:
x ||= y is identical to x = x || y. The ||= operator guarantees that no assignment occurs if x is truthy. This distinction is critical when the left operand is a property with a setter; ||= will not trigger the setter unless the underlying value is falsy, whereas x = x || y triggers the setter unconditionally.
Falsy Evaluation
The assignment is executed if and only if the left operand evaluates to one of the following JavaScript falsy values:false0or-00n(BigInt zero)""(empty string)nullundefinedNaN
TypeScript Type Constraints
In TypeScript, the type of the right operand must be assignable to the type of the left operand. Because the operator handles falsy values, the left operand’s type typically includes a union of a truthy type and a falsy type (likenull, undefined, or 0).
Evaluation Order
- The left operand is evaluated.
- The left operand is coerced to a boolean.
- If the boolean evaluates to
true(truthy), the operation short-circuits, returning the left operand’s value. - If the boolean evaluates to
false(falsy), the right operand is evaluated. - The result of the right operand is assigned to the left operand, and that value is returned.
Master TypeScript with Deep Grasping Methodology!Learn More





