Debugging Microservices & Distributed Systems
3 min read

JavaScript's &&= Operator: Understanding Logical AND Assignment

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.


Related Articles

If you enjoyed this article, you might find these related pieces interesting as well.

Recommended Engineering Resources

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Imagine where you would be in two years if you actually took the time to learn every day. A little effort consistently adds up, shaping your skills, opening doors, and building the career you envision. Start now, and future you will thank you.


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.

Interested in a partnership? Shoot me an email at hi [at] trevorlasn.com with all relevant information.