Skip to main content
The logical OR assignment (||=) 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 encounters x ||= y, it performs the following steps:
  1. Evaluates the left-hand side (LHS) operand.
  2. Coerces the LHS value to a boolean.
  3. If the LHS is truthy, the operation short-circuits. The right-hand side (RHS) is never evaluated, and no assignment takes place.
  4. If the LHS is falsy, the engine evaluates the RHS and assigns its resulting value to the LHS.
In JavaScript, the following values are falsy and will trigger the assignment: false, 0, -0, 0n, "" (empty string), null, undefined, and NaN.

Logical Equivalence

The ||= operator is strictly equivalent to the following logical expression:
It is a common misconception that 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.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More