# Chaining assignment operator

**URL:** <https://es.discourse.group/t/chaining-assignment-operator/670>\
**Category:** 💡 Ideas\
**Created:** [February 25, 2021, 8:54pm UTC](https://es.discourse.group/t/chaining-assignment-operator/670 "2021-02-25T20:54:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vrugtehagel](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/vrugtehagel/32/653_2.png) [@vrugtehagel](https://es.discourse.group/u/vrugtehagel)\
**Post date:** [February 25, 2021, 8:54pm UTC](https://es.discourse.group/t/chaining-assignment-operator/670/1 "2021-02-25T20:54:35Z")

</div>

There's a bunch of assignment operators already: a few are listed below.

- Bitwise assignment operators such as `^=`, `&=`, `|=`, `>>>=`, and a few more;
- Mathematical assignment operators such as `+=`, `/=`, `**=`, `%=`, and a few more;
- Logical assignment operators `&&=`, `??=`, `||=`

These syntaxes all come down to the following being equivalent:

```javascript
foo [operator]= bar
// is equivalent to
foo = foo [operator] bar

```

Sometimes, I wish I would have this for chaining; i.e. shortening `foo = foo.bar` to `foo .= bar`. I don't think this syntax could be misinterpreted (not by parsers, anyways - I could see some people coming from PHP confusing it for string concatenation).

Two example situations in which this would be helpful:

```javascript
products .= filter(product => product.stock > 0);

```

and (slightly more complex):

```javascript
let node = someElement;
let index = 0;
while(node){
    node .= previousSibling;
    index++;
}

```

Perhaps this is extendable to include the optional chaining assignment operator, `?.=` (where `foo ?.= bar` would be equivalent to, you guessed it, `foo = foo?.bar`).

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 26, 2021, 9:40am UTC](https://es.discourse.group/t/chaining-assignment-operator/670/2 "2021-02-26T09:40:43Z")

</div>

Hi @vrugtehagel

Possible duplicate of [.= Operator](https://es.discourse.group/t/operator/586)

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [February 26, 2021, 3:02pm UTC](https://es.discourse.group/t/chaining-assignment-operator/670/3 "2021-02-26T15:02:00Z")

</div>

I do like the idea of being able to use functions such as `filter()` in a self-modifying way.

But, I do find the syntax a bit confusing, because, unlike other operators, the "." takes an identifier on the right, not a value.

```javascript
const filter = () => ...
x += filter(...) // Uses the local filter function
x ??= filter(...) // Uses the local filter function
x .= filter(...) // This filter is a property of x

x += (fn1() + fn2()) // Valid syntax
x ??= (fn1() + fn2()) // Valid syntax
x .= (fn1() + fn2()) // Doesn't work, .= needs an identifier to operate on, not a value.

```
