Array destructuring with JavaScript

In our last post, we saw how to implement object destructuring. A similar feature is available for arrays and replaces the following syntax:

With destructuring, we can simplify that code into:

The only difference with object destructuring is the usage of square brackets [] instead of curly ones {}. Default values are supported, too:

And if you want to ignore one or more items in the array, you can leave blank spaces instead of variable names:

You can find more examples in the MDN web docs here.

Alain Chautard

Alain is a Google Developer Expert in Web Technologies, Angular, and Google Maps. His daily mission is to help development teams adopt Angular and build at scale with the framework. He has taught Angular on all six continents! A world traveler and photographer, Alain is also an international conference speaker, and a published author of several video courses.