---
title: "JavaScript &&= Operator: Logical AND Assignment Explained"
description: "Use the &&= operator to safely update truthy values while preserving falsy states"
canonical: "https://www.trevorlasn.com/blog/javascript-logical-and-assignment-operator"
author: "Trevor I. Lasn"
published: "2024-11-05"
last-updated: "2024-11-05"
topics: ["JavaScript"]
---

# JavaScript &&= Operator: Logical AND Assignment Explained

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

By Trevor I. Lasn · Published 2024-11-05 · https://www.trevorlasn.com/blog/javascript-logical-and-assignment-operator

The logical AND assignment operator `&&=` arrived alongside [??= in ECMAScript 2021.](https://262.ecma-international.org/12.0/index.html) 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:

```js frame="code" title="JavaScript"
// Traditional if statement
if (x) {
  x = y;
}

// Using logical AND with assignment
x = x && y;

// Modern &&= operator (ES2021)
x &&= y;
```

<div
  class="flex items-center text-sm pl-4 pr-2 mb-5 mt-5 text-indigo-700 rounded-lg bg-surface dark:text-indigo-300"
  role="note"
>
  <svg 
    class="flex-shrink-0 inline w-4 h-4 me-3" 
    aria-hidden="true"
    xmlns="http://www.w3.org/2000/svg"
    fill="none"
    viewBox="0 0 24 24"
    stroke="currentColor"
    stroke-width="2"
  >
    <path d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
  </svg>
  <div>
    <a href="/blog/javascript-truthy-and-falsy"
        class="font-medium text-md mb-1 text-indigo-700 dark:text-indigo-300 "
      >
      JavaScript Truthy and Falsy: A Deep Dive
    </a>
  </div>
</div>

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

```js frame="code" title="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:

```js frame="code" title="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:

```js frame="code" title="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:

```js frame="code" title="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.

<div
  class="flex items-center text-sm pl-4 pr-2 mb-5 mt-5 text-indigo-700 rounded-lg bg-surface dark:text-indigo-300"
  role="note"
>
  <svg 
    class="flex-shrink-0 inline w-4 h-4 me-3" 
    aria-hidden="true"
    xmlns="http://www.w3.org/2000/svg"
    fill="none"
    viewBox="0 0 24 24"
    stroke="currentColor"
    stroke-width="2"
  >
    <path d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
  </svg>
  <div>
    <a href="/blog/javascript-nullish-coalescing-assignment-operator"
        class="font-medium text-md mb-1 text-indigo-700 dark:text-indigo-300 "
      >
      JavaScript's ??= Operator: Default Values Made Simple
    </a>
  </div>
</div>
