Course contentsShow
← JavaScript
Lesson 249 of 2,7237. Arithmetic and OperatorsFree lesson

Logical Assignment Operators

Combine logical operations with assignment using &&=, ||=, and ??= for conditional updates.

Logical Assignment Operators

You'll learn how to combine logical checks with assignment in a single, clean operation.

What Are Logical Assignment Operators?

Logical assignment operators blend the power of logical operators (&&, ||, ??) with assignment (=). Instead of writing a condition and then an assignment separately, you do both at once. Think of them as shortcuts that say: "Only update this variable if a certain logical condition is met."

The Three Operators

&&= (Logical AND Assignment)

Assigns only if the left side is truthy:

let user = "Alice";
user &&= "Bob"; // user is truthy, so assign "Bob"
console.log(user); // "Bob"

let score = 0;
score &&= 100; // score is falsy (0), so no assignment
console.log(score); // 0

||= (Logical OR Assignment)

Assigns only if the left side is falsy:

let username = "";
username ||= "Guest"; // username is falsy, so assign "Guest"
console.log(username); // "Guest"

let name = "Alice";
name ||= "Guest"; // name is truthy, so no assignment
console.log(name); // "Alice"

??= (Nullish Coalescing Assignment)

Assigns only if the left side is null or undefined:

let count = 0;
count ??= 10; // count is 0 (not null/undefined), so no assignment
console.log(count); // 0

let value = null;
value ??= 5; // value is null, so assign 5
console.log(value); // 5

Why Use Them?

They make conditional updates cleaner and more expressive. Compare username ||= "Guest" to username = username || "Guest" — same result, less repetition.

Key Takeaway: Logical assignment operators (&&=, ||=, ??=) update variables only when specific logical conditions are met, combining logic and assignment into one concise operation.