๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-destructuring-and-spread-operator-explained
JavaScript Destructuring and the Spread Operator โ€“ Explained with Example Code
August 10, 2021 - Here, we are copying array1 into array2 using the spread operator. We are logging array2 in the console, and we are getting the items of array1. That's all, folks! In this article, we learned about array and object destructuring and the spread operator.
๐ŸŒ
Bluethinkinc Blog
blog.bluethinkinc.com โ€บ home โ€บ destructuringย in javascript (array, object, and spread operator)
Destructuring in JavaScript (Array, Object, and Spread Operator) - Bluethinkinc Blog
May 21, 2025 - ... I hope this blog found to be useful for you. If you have any queries feel free to ask. Destructuring is a powerful ES6 new feature to make your code simpler, clearer, and beautiful.
Discussions

What is the time complexity of array de-structuring?
With de-structuring, for example, this: const [a, b] = array; You are implicitly specifying the index, starting from zero and following the order of each new variable you define (a, b and so on) so this should be O(1) or constant time. Source. On the other hand if you do: const [a, b, ...rest] = array; The ...rest part (spread syntax) will have to loop through the rest of array to extract each element and put it in rest, so this will be O(n), with n being the number of remaining elements. Source. More on reddit.com
๐ŸŒ r/learnjavascript
29
22
December 16, 2022
How to get the last item of an array with destructuring

Why not just do array[array.lemth -1]

More on reddit.com
๐ŸŒ r/javascript
22
6
June 13, 2020
๐ŸŒ
Web Dev Simplified
blog.webdevsimplified.com โ€บ 2020-08 โ€บ destructuring-and-spread
How To Use Destructuring And The Spread Operator? (One Of The Best JS Features)
August 24, 2020 - The spread operator is a tool that lets you spread out all the elements of an array or object. This can be used to create new objects or arrays that are clones, but for our specific use case we can combine this with destructuring to get all other elements not specifically destructured already...
๐ŸŒ
Tntra
tntra.io โ€บ home โ€บ understating destructuring, resting, and spread operators in modern javascript
Understating Destructuring, Resting, and Spread Operators in Modern JavaScript - Tntra
February 7, 2025 - We will use the same destructuring object principle again! The Output when you log into the movie โ€œcivil warโ€. JavaScript uses three dots (โ€ฆ) for the rest and spread operators.
๐ŸŒ
Medium
medium.com โ€บ @reigndesign โ€บ javascript-destructuring-and-the-spread-operator-fcb722539bad
JavaScript Destructuring and the Spread Operator | by Adetunji Adeleye | Medium
February 18, 2025 - In this post, weโ€™ll explore these ... potential in your own projects. ... Destructuring in JavaScript is a syntax that allows you to assign variables directly from arrays or objects....
๐ŸŒ
Medium
medium.com โ€บ @arkajd95 โ€บ day-6-react-destructuring-and-spread-operator-in-javascript-3c3b4fd3bfeb
Day-6: React โ€” Destructuring and Spread Operator in Javascript | by Arka Das | Medium
May 6, 2025 - Use destructuring when you want to access multiple properties from an object without repeating the object name each time. ... const colors = ["red", "green", "blue"]; const [primary, secondary] = colors; console.log(primary); // "red" ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ understanding-destructuring-rest-parameters-and-spread-syntax-in-javascript
Understanding Destructuring, Rest Parameters, and Spread Syntax in JavaScript | DigitalOcean
August 27, 2021 - Introduced in ECMAScript 2015 (ES6), destructuring, rest parameters, and spread syntax provide more direct ways of accessing the members of an array or an obโ€ฆ
Find elsewhere
Top answer
1 of 4
4

It is executed like following

var a = [1, 2, 3];
[x, y, ...a ] = [0, ...a, 4];
[x, y, ...a ] = [0, 1, 2, 3, 4];

which means first value in RHS array is assigned to x, second value in RHS array is assigned to y and the remaining values are assigned to a.

Hence, value of x is 0, y is 1 and a is [2, 3, 4]

2 of 4
3

It's not clear to me, though, how the ...a get assigned to 4.

It's not.

Lets split things up a little:

On the right hand side of the assignment you are using an array literal with a spread element. The value of a is "flattened" into the new array.

Thus, [0, ...a, 4] is is equivalent to [0].concat(a, [4]). The result is a new array.

var a = [1, 2, 3];

console.log('spread element', [0, ...a, 4]);
console.log('concat', [0].concat(a, [4]));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

On the left hand side you are using array destructuring with a rest element. [x, y, ...a ] means

  • assign the first value of the iterable to x
  • assign the second value of the iterable to y
  • assign the remaining values of the iterable as an array to a

These two are equivalent:

var a = [1,2,3,4];
var [x, y, ...z] = a;
console.log('destructuring', x, y, z);

var x = a[0];
var y = a[1];
var z = a.slice(2);
console.log('manual + slice', x, y, z);
Run code snippetEdit code snippet Hide Results Copy to answer Expand


Of course combining these two is perfectly fine. In an assignment, the left hand side doesn't care what how the right hand side is computed and vice versa.

What's confusing about your example is that you are using a again in the destructuring assignment, but that's the same as overriding the value of a with a new value. However the end result is

  • [0, ...a, 4] results in [0,1,2,3,4] therefor
  • x has value 0
  • y has value 1
  • a has value [2,3,4]

In fact, JS throws an exception when doing: ...a = 4;

The error message you are getting is strange. It should really be just a syntax error.

... by itself doesn't mean anything. It's not an operator, it's a punctuator (like ; or ,) that has a different meaning depending on the context it is used (and allowed).

See also What is SpreadElement in ECMAScript documentation? Is it the same as Spread operator at MDN?

๐ŸŒ
Abdul Wahab Junaid
awjunaid.com โ€บ home โ€บ destructuring and spread/rest operators in javascript
Destructuring and Spread/Rest Operators in JavaScript | Abdul Wahab Junaid
April 9, 2024 - Destructuring is commonly used in function parameters to extract values from objects or arrays passed to the function. The spread operator (...) and rest parameter allow you to work with arrays and objects in a more concise and versatile way.
๐ŸŒ
Medium
medium.com โ€บ @mvmilanjain โ€บ javascript-destructuring-spread-operator-your-codes-new-best-friends-cd8c769f687a
JavaScript Destructuring & Spread Operator: Your Codeโ€™s New Best Friends ๐Ÿš€ | by Mvmilanjain | Medium
August 9, 2025 - In the age of React, Vue, and functional programming, immutability is king. Spread operators let you create new objects and arrays without mutating the originals โ€” your state management will thank you!
๐ŸŒ
Medium
chanduthedev.medium.com โ€บ what-is-destructuring-rest-and-spread-operator-in-javascript-a998ab89b86e
What is Destructuring, Rest and Spread operator in JavaScript? | by chanduthedev | Medium
November 25, 2023 - Spread operator syntax is similar to Rest property in array de-structuring, But Spread will split into individual elements whereas rest collects into an array
๐ŸŒ
DEV Community
dev.to โ€บ thekrprince โ€บ spread-operator-rest-operator-destructuring-in-js-4nbg
Spread operator, Rest Operator & Destructuring in JS - DEV Community
May 8, 2021 - It's also next gen javascript feature. Destructuring easily extract array elements or object properties and store them in variables. By the above definition, you'd have been thinking that It works exactly as spread operator but It's work differently.
๐ŸŒ
Medium
medium.com โ€บ @mojahidulislamt17 โ€บ es6-in-javascript-spread-operator-destructuring-and-more-693b7c0b5a7a
ES6 in JavaScript: Spread Operator, Destructuring, and More | by Mojahidul Islam | Medium
August 5, 2025 - Using the spread operator creates ... [10, 20, 30, 40] ... Destructuring allows you to unpack values from arrays or properties from objects into distinct variables, making code more readable and clean....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-object-destructuring-spread-operator-rest-parameter
JavaScript Object Destructuring, Spread Syntax, and the Rest Parameter โ€“ A Practical Guide
February 8, 2021 - While spread syntax helps expand or spread elements and properties, the rest parameter helps collect them together. In the case of objects, the rest parameter is mostly used with destructuring syntax to consolidate the remaining properties in ...
๐ŸŒ
W3Schools
w3schools.in โ€บ javascript โ€บ spread-operator
JavaScript Spread Operator - W3Schools
The JavaScript spread operator (...) is a versatile and powerful tool that simplifies complex operations in your code. It enables you to expand or 'spread' arrays or objects, making your code more readable and efficient.