JavaScript &&= Operator: Logical AND Assignment Explained

Use the &&= operator to safely update truthy values while preserving falsy states

The logical AND assignment operator &&= arrived alongside ??= in ECMAScript 2021. It combines logical AND && with assignment =, offering a shorthand way to conditionally update values.

The &&= operator is a logical assignment operator that updates values based on truthiness. It only assigns the new value if the existing value is truthy. Here’s how it works under the hood:

JavaScript
// Traditional if statement
if (x) {
x = y;
}
// Using logical AND with assignment
x = x && y;
// Modern &&= operator (ES2021)
x &&= y;

The behavior of &&= becomes clear when we examine different initial values.

JavaScript
let access = true;
access &&= 'granted'; // access becomes 'granted'
access = false;
access &&= 'granted'; // access stays false
access = '';
access &&= 'granted'; // access stays empty string
access = 0;
access &&= 'granted'; // access stays 0

Starting with true (truthy), the value changes to ‘granted’; but with false, an empty string, or 0 (all falsy values), the original value stays unchanged.

This demonstrates how &&= only performs assignment when the existing value is truthy, making it ideal for conditional updates where you want to preserve falsy states.

The &&= operator excels at handling conditional updates where you want to respect falsy values. Here’s a common use case with user permissions:

JavaScript
function updateUserAccess(user) {
// Only updates permissions if they already exist
user.canEdit &&= checkPermissions();
user.canDelete &&= checkAdminStatus();
return user;
}

The &&= operator is also useful for managing application states and validation:

JavaScript
const form = {
isValid: true,
isSubmitted: false,
hasErrors: false
};
// Only validate if form is currently valid
form.isValid &&= validateFields(); // Runs validation
form.isSubmitted &&= submitToServer(); // Skipped if not valid
form.hasErrors &&= checkErrors(); // Preserves false state

Or for an API response pattern:

JavaScript
const response = {
isAuthenticated: true,
hasPermission: true,
isExpired: false
};
// Each check only runs if previous checks pass
response.isAuthenticated &&= validateToken();
response.hasPermission &&= checkAccess();
response.isExpired &&= checkExpiration(); // Stays false if no permission

Keep in mind that the &&= operator is about conditional updates based on truthiness. If you need to handle null or undefined specifically, consider using the ??= operator instead.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
4 min read

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DurationFormat API for internationalized time displays

Mar 13, 2025
Read article
JavaScript
9 min read

Deep dive into JavaScript Symbols - what they are, why they matter, and how to use them effectively

Nov 15, 2024
Read article
JavaScript
7 min read

AggregateError holds several errors in one, the way Promise.any() reports that every promise rejected. How to throw and read it, plus the new SuppressedError.

Sep 2, 2024
Read article
JavaScript
6 min read

Closures are essential for creating functions that maintain state, without relying on global variables.

Sep 6, 2024
Read article
JavaScript
3 min read

Copy and paste text, images, and files using the new navigator.clipboard API

Dec 7, 2024
Read article
JavaScript
7 min read

How was JavaScript written back in the day? I dug into early frameworks and libraries to see how code looked then and what still holds up.

Jun 12, 2025
Read article
JavaScript
7 min read

JavaScript has eight falsy values: false, 0, -0, 0n, an empty string, null, undefined and NaN. Empty arrays and objects are truthy. The edge cases, explained.

Oct 27, 2024
Read article
JavaScript
7 min read

|| returns the first truthy value, && the first falsy one, and ?? falls back only on null or undefined. When to use each, and the cases that trip people up.

Oct 26, 2024
Read article
JavaScript
8 min read

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/javascript-logical-and-assignment-operator. It was written by a human and polished using grammar tools for clarity.