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 only if the left operand is falsy.
Mechanics and Short-Circuit Evaluation
The operator relies on short-circuit evaluation. When the JavaScript engine encountersx ||= y, it performs the following steps:
- Evaluates the left-hand side (LHS) operand.
- Coerces the LHS value to a boolean.
- If the LHS is truthy, the operation short-circuits. The right-hand side (RHS) is never evaluated, and no assignment takes place.
- If the LHS is falsy, the engine evaluates the RHS and assigns its resulting value to the LHS.
false, 0, -0, 0n, "" (empty string), null, undefined, and NaN.
Logical Equivalence
The||= operator is strictly equivalent to the following logical expression:
x ||= y is equivalent to x = x || y. While the resulting value is often the same, the underlying mechanics differ significantly. The expression x = x || y always performs an assignment operation, whereas x ||= y conditionally performs the assignment.
Syntax Visualization
Impact on Object Setters
Because||= only performs an assignment if the LHS is falsy, it prevents unnecessary setter invocations on objects. This is the primary technical distinction between ||= and standard assignment combined with a logical OR.
Master JavaScript with Deep Grasping Methodology!Learn More





