What is Array destructuring in JavaScript?

Destructuring in JavaScript is a technique that lets you extract data from arrays or objects into distinct variables. You can use destructuring to extract a single value, multiple values, or all the values in an array.

Destructuring is available in most modern programming languages. In JavaScript, destructuring is supported by both the ES6 and ES2015 standards.

Here's an example of destructuring an array into multiple variables:

var colors = ['red', 'green', 'blue']
var [red, green, blue] = colors

This code assigns the values 'red', 'green', and 'blue' to the variables red, green, and blue.

You can also use destructuring to extract a single value from an array:

var colors = ['red', 'green', 'blue']
var color = colors[0]

This code extracts the first value from the colors array and assigns it to the color variable.

destructuring works with objects too:

var person = {
    name: 'Nate',
    age: 33
}

var {name, age} = person

This code extracts the name and age properties from the person object and assigns them to the name and age variables.

Here's a quick video to help supplement the above explanation of what array destructuring is.

💡
Need a writer? Want to work with me? I have a subscription copywriting service available at Zipply.io. Subscribe to a set number of words and split it into as many copy assets as you need. Scale your startup, small business, or MVP. Upgrade, downgrade and pause at any time. Start your subscription here.