Skip to content

Logical Assignment

In one line

Logical assignment operators (||=, &&=, ??=) combine a logical check with assignment in one operator.

In simple words

Logical assignment operators combine a logical check and an assignment into one concise operator. ||= assigns only if the left is falsy, &&= assigns only if the left is truthy, and ??= assigns only if the left is null or undefined. They are shorthand for common patterns.

Before: if (!a) a = default; or a = a || default;. With logical assignment: a ||= default;. The operator only evaluates the right side if the condition holds, so it is lazy — no unnecessary computation or side effects.

These were added in ES2021. They are especially useful for initializing config objects, setting defaults on possibly-empty fields, and conditional mutation. The ??= variant is the nullish-aware default setter.

Example

javascript
let count = 0;
count ||= 10; // count is falsy (0), so assigns 10
console.log(count); // 10

let name = "Ada";
name &&= name.toUpperCase(); // truthy, so assigns uppercased
console.log(name); // "ADA"

let config = null;
config ??= { port: 3000 }; // null, so assigns default
console.log(config); // { port: 3000 }

||= assigns when falsy, &&= when truthy, ??= when nullish. The right side is only evaluated if the condition holds.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary