TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
??= (logical nullish assignment) operator evaluates its right-hand side (RHS) operand and assigns the resulting value to its left-hand side (LHS) operand strictly if, and only if, the LHS operand evaluates to a nullish value (null or undefined).
=), ??= utilizes short-circuit evaluation. If the LHS operand evaluates to any non-nullish value—including falsy values like 0, "" (empty string), NaN, or false—the RHS expression is completely ignored, and the assignment operation is bypassed.
Crucially, the ??= operator guarantees that the LHS expression is evaluated exactly once. This makes it semantically distinct from both LHS = LHS ?? RHS and LHS ?? (LHS = RHS):
LHS = LHS ?? RHSalways performs an assignment, which can trigger unwanted side effects in property setters even if the value remains unchanged.LHS ?? (LHS = RHS)evaluates the LHS reference twice if the initial evaluation is nullish.
??= prevents unintended side effects such as double-invocation of getters, redundant function calls in property access chains, or multiple increments in index expressions.
Type System Behavior
Under standard strict mode (strictNullChecks: true), TypeScript performs control flow analysis and type narrowing on the ??= operator. After the operation, the type of the LHS is narrowed to exclude null and undefined if the RHS is guaranteed to be non-nullish.
TypeScript permits the use of ??= even if the LHS type does not include null or undefined. In such cases, the compiler simply infers that the RHS is unreachable at runtime and leaves the LHS type unchanged, compiling successfully without emitting an error.
The following examples demonstrate the assignment mechanics and type narrowing across different types and values:
Short-Circuiting and RHS Side Effects
Because of short-circuit evaluation, any function calls or operations on the RHS are deferred and ultimately skipped if the LHS is non-nullish. This guarantees that computationally expensive operations or state-mutating functions are not executed unnecessarily.Master TypeScript with Deep Grasping Methodology!Learn More





