Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Logical Assignment

Logical assignment operators conditionally assign values using ||=, &&=, or ??=. They short-circuit, so the right-hand expression runs only when assignment is needed.

How it works

Logical assignment operators combine a logical test with assignment. ||= assigns when the left side is falsy, &&= assigns when it is truthy, and ??= assigns only when it is null or undefined; the right side is skipped when assignment is unnecessary.

Syntax

left ||= right
left &&= right
left ??= right

Parameters

NameTypeDescription
leftassignable referenceLeft-hand variable, property, or other assignable reference tested by the operator.
rightexpressionExpression evaluated only when the operator's condition allows assignment.

Returns

The assignment expression evaluates to the resulting value of the left-hand reference.

Key points

  • Logical assignment extends ordinary assignment with a condition, avoiding a separate read-and-write expression.
  • The three logical assignment forms are &&=, ||=, and ??=.
  • Logical operators select operand values rather than necessarily producing Boolean results, so the assigned value can be any expression result.

Example: Conditional assignments

let displayName = "";
displayName ||= "Guest";

let retries;
retries ??= 3;

let ready = true;
ready &&= false;

console.log(displayName, retries, ready);

Each operator conditionally assigns a fallback or replacement value based on the left-hand operand.

When to use it

Fallback defaults

Provide defaults for empty strings, zero-like values, or other falsy configuration values.

Nullish initialization

Initialize a value only when it is null or undefined without replacing valid falsy values.

Conditional updates

Update a value only when an existing value passes a truthiness check.

  • Logical operators
  • Assignments
  • Nullish coalescing operator
  • Short-circuit evaluation

Source: JavaScript specials. Checked 2026-10-15.