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.