The logical AND 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 its right operand and assigns the resulting value to its left operand strictly if the left operand is truthy. It utilizes short-circuit evaluation, meaning the right operand is neither evaluated nor assigned if the left operand evaluates to a falsy value.
x && (x = y), the &&= operator has a critical semantic distinction: it evaluates the left-hand side reference exactly once. The expression x && (x = y) evaluates the left-hand side twice. This single-evaluation guarantee is vital when the left operand contains side effects (e.g., arr[i++] or obj.getter()), as it prevents unintended double execution.
Furthermore, the &&= operator guarantees that no assignment operation occurs if the left operand is falsy (false, 0, -0, 0n, "", null, undefined, or NaN). This distinction prevents unnecessary write operations, which is critical when the left operand is a property with a setter or a Proxy trap.
Evaluation Behavior
- The left operand is evaluated exactly once.
- If the left operand is falsy, the operation short-circuits. The right operand is not evaluated, and no assignment takes place.
- If the left operand is truthy, the right operand is evaluated, and its value is assigned to the left operand.
TypeScript Type Resolution
In TypeScript’s type system, Control Flow Analysis calculates the resulting narrowed type of the left operand by combining its potential falsy states with the type of the right operand. Because the assignment only happens when the left operand is truthy, any types representing falsy values (likenull or undefined) in the left operand’s original union type are preserved in the resulting narrowed type.
any, or unknown). If the variable’s declared type does not accept the right operand’s type, the TypeScript compiler will throw a ts(2322) error.
Master TypeScript with Deep Grasping Methodology!Learn More





