Skip to main content

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.

The logical AND assignment (&&=) 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.
leftOperand &&= rightOperand;
While logically similar to the expression 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

  1. The left operand is evaluated exactly once.
  2. If the left operand is falsy, the operation short-circuits. The right operand is not evaluated, and no assignment takes place.
  3. If the left operand is truthy, the right operand is evaluated, and its value is assigned to the left operand.
let truthyValue: string | null = "initial";
truthyValue &&= "updated"; 
// Evaluates to "updated". Assignment occurs.

let falsyValue: string | null = null;
falsyValue &&= "updated"; 
// Evaluates to null. Short-circuits; no assignment occurs.

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 (like null or undefined) in the left operand’s original union type are preserved in the resulting narrowed type.
// Initialized with a value that could be undefined to prevent immediate narrowing
let numericState: number | undefined = Math.random() > 0.5 ? 10 : undefined;
const nextState: number = 50;

// TypeScript evaluates the narrowed type of numericState after this operation
// as 'number | undefined'. 
numericState &&= nextState; 

// If numericState was undefined, it bypassed assignment and remains undefined.
// If numericState was a number, it received the new number type.
TypeScript’s Control Flow Analysis updates the narrowed type of the variable after the operation, but it never expands the declared type. The variable must already have a declared type broad enough to accept the right operand’s type (such as a matching union, 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.
let mixedState: string | boolean = Math.random() > 0.5 ? "active" : false;

// Valid: The right operand is a boolean, which satisfies the declared union type.
// The narrowed type of mixedState after this line becomes 'boolean'.
mixedState &&= false; 

let strictState: string = "active";

// Error ts(2322): Type 'boolean' is not assignable to type 'string'.
strictState &&= false; 
Master TypeScript with Deep Grasping Methodology!Learn More