JavaScript Logical assignment

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Logical assignment operators update a variable only in certain cases: ||= when falsy, &&= when truthy, ??= when null or undefined.

JavaScript Syntax

JavaScript
let a = null;
a ??= "default";
console.log(a);
let b = 0;
b ||= 5;
console.log(b);
Try it Editor

How It Works

  1. ??=. Fills nullish a.
  2. ||=. 0 is falsy, so b becomes 5.

Examples

Example 1 — Keep 0 with ??=

0 is not nullish.

JavaScript
let n = 0;
n ??= 10;
console.log(n);
Try it Editor

How It Works

  1. ??= does not replace 0.

Example 2 — &&=

Assign when left is truthy.

JavaScript
let x = { ok: true };
x &&= { ok: false };
console.log(x.ok);
Try it Editor

How It Works

  1. Truthy left is replaced.

Example 3 — Vs ?? and ||

Same idea as expressions.

JavaScript
let v = null;
v = v ?? "x";
console.log(v);
Try it Editor

How It Works

  1. ??= is shorter for assignment.

Frequently Asked Questions

Prefer ??= so 0 and "" are kept when they are valid values.
Yes—ES2021. Supported in modern browsers and Node.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful