# Spread Assignment Operator

**URL:** <https://es.discourse.group/t/spread-assignment-operator/463>\
**Category:** 💡 Ideas\
**Created:** [September 13, 2020, 2:27am UTC](https://es.discourse.group/t/spread-assignment-operator/463 "2020-09-13T02:27:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MidSpike](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/midspike/32/433_2.png) [@MidSpike](https://es.discourse.group/u/MidSpike)\
**Post date:** [September 13, 2020, 2:27am UTC](https://es.discourse.group/t/spread-assignment-operator/463/1 "2020-09-13T02:27:19Z")

</div>

## I'm proposing a spread-like operator `...=` for (re)assignment on Arrays or Objects

### Similar to the existing convention of regenerative consolidation:

```javascript
/* Arrays */
let x = [1, 2, 3];
x = [...x, 4, 5, 6];
// Result: [1, 2, 3, 4, 5, 6]

```

```javascript
/* Objects */
let y = {a:1, b:2, c:3};
y = {...y, {b:5, c:6, d:7}}
// Result: {a:4, b:5, c:6, d:7}

```

### I propose the following shorthand syntax for spread (re)assignment

```javascript
/* Arrays will act in a manner of bulk appending values */
let x = [1, 2, 3];
x ...= [4, 5, 6];
// Logic: [1, 2, 3, ...[4, 5, 6]];
// Result: [1, 2, 3, 4, 5, 6]

```

```javascript
/* Objects will act in a manner of bulk `property:value` (re)assignment */
let y = {a:1, b:2, c:3};
y ...= {b:5, c:6, d:7};
// Logic: {a:1, b:2, c:3, ...{b:5, c:6, d:7}};
// Result: {a:4, b:5, c:6, d:7}

```

#### ~~With this proposed syntax, I suggest the ability to (re)populate Arrays and Objects (using this operator) without having to fully re-assign the variable, thus allowing for usage on arrays and objects declared using `const`~~

#### The following syntax will only work on variables declared using `var` and `let`. It will throw an error for variables declared using `const`

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [September 13, 2020, 1:44pm UTC](https://es.discourse.group/t/spread-assignment-operator/463/2 "2020-09-13T13:44:03Z")

</div>

An assignment will never work on a `const` variable. If you really suggest that `...=` should modify the lefthandside object/array, this functionality already exists:

```javascript
const x = [1, 2, 3];
x.push(4, 5, 6); // or `x.push(...array)`

const y = {a:1, b:2, c:3};
Object.assign(y, {b:5, c:6, d:7});

```

---

<div class="post-metadata">

**Author:** ![MidSpike](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/midspike/32/433_2.png) [@MidSpike](https://es.discourse.group/u/MidSpike)\
**Post date:** [September 13, 2020, 4:03pm UTC](https://es.discourse.group/t/spread-assignment-operator/463/3 "2020-09-13T16:03:52Z")

</div>

I modified my examples to focus on the main intentions of the proposal and drop the request for `const` compatibility
