From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Destructuring arrays

Destructuring arrays - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Destructuring arrays

- [Instructor] Destructuring assignment gives us an easy way to extract data from arrays and objects and assign them to variables. Let's take a look at how destructuring works with arrays. Here I have an array of cities. Now if I just wanted to grab a couple of these cities, I could access them by their index. So I could say city zero, that'll give me Spokane. But a new way to handle this type of access is to handle it with destructuring. Let's update cities and instead I'm going to use the array brackets here and I'm going to create a variable name. I'll call it first. Now I can console log first. Then that's going to give me the first item that's in the array. If I wanted to grab the second item in the array, I could. I can console log second and that'll give me Boston. Now these variable names are whatever you want to name them, so I could call this fifth, but that wouldn't mean it's the fifth one. It just…

Contents