---
title: "Logical Assignment Operators in JavaScript"
description: "Combine logical operators and assignment expressions with ||=, and and =, and ??=. Practical notes and code examples on."
url: "https://h3manth.com/scribe/logical-assignment-operators-in-javascript/"
canonical_url: "https://h3manth.com/scribe/logical-assignment-operators-in-javascript/"
markdown_url: "https://h3manth.com/scribe/blog/logical-assignment-operators-in-javascript.md"
publishDate: "2020-08-29T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","es2021","tc39"]
word_count: 172
reading_time_minutes: 1
x_markdown_tokens: 374
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Logical Assignment Operators in JavaScript

> Combine logical operators and assignment expressions with ||=, and and =, and ??=. Practical notes and code examples on.

Logical assignment operators in JavaScript combine Logical Operators and Assignment Expressions.

## The Operators

```javascript
// "Or Or Equals"
x ||= y;
x || (x = y);

```javascript
// "And And Equals"
x &&= y;
x && (x = y);

```javascript
// "QQ Equals" (Nullish Coalescing)
x ??= y;
x ?? (x = y);
```

```

```

## Example: updateID Function

Say you have a function `updateID`, it can vary in the following ways:

```javascript
const updateID = user => {
  // We can do this
  if (!user.id) user.id = 1

  // Or this
  user.id = user.id || 1

  // Or use logical assignment operator
  user.id ||= 1
}
```

```

```

## Using with Nullish Coalescing

You could also use it with `??`:

```javascript
function setOpts(opts) {
  opts.cat ??= 'meow'
  opts.dog ??= 'bow';
}

setOpts({ cat: 'meow' })
```

This is on [stage-4](https://dev.to/hemanth/stage-4-features-5a26) and you should be able to use it today!

I am happy that I was able to [co-champion](https://github.com/tc39/proposal-logical-assignment#champions) this proposal. [7 Years ago](https://esdiscuss.org/topic/shorthand-notation-for-attribute-creation) it was just a thought!
