Destructuring Assignment in ES6

Share this video with your friends

Send Tweet

ECMAscript 6 destructuring provides flexible options for variable assignment.

Dipankar
Dipankar
~ 8 years ago

Destructuring is a shortcut syntax. Assignment of Skyler to Skyler object forced me to rewind.

Zhenyang Hua
Zhenyang Hua
~ 8 years ago

Is destructuring often used in modules import ? e.g. import { XX } from 'xxx/core' ?

Stephan Maier-Knodt
Stephan Maier-Knodt
~ 8 years ago

Hi, it is a pity that you mix old ECMA 5 and ECMA 6 script together in every session of the course "Learn ES6 (ECMAScript 2015)". So it would be nice to just use "let" instead of "var" always. And for sure some the other stuff you very nicely introduce. So would would have a much better learning effect.

But I love your way of introducing things in short videos.

Jesse
Jesse
~ 8 years ago

At about the two minute mark, when you use state:location the state becomes part of your url on reload. The page returns a file not found error as es6/index.html becomes es6/New%20York. So location is a keyword to be avoided.

Gene
Gene
~ 8 years ago

Code in transcript is wrong.

var [,Skyler] = people;

Should be:

var [Skyler,] = people;

Mike
Mike
~ 8 years ago

Great examples. Your videos are one of the reasons I subscribe to Egghead.

Lluís Peinado
Lluís Peinado
~ 7 years ago

Hi, I am playing around with the examples. Just wanted to add a second parameter to the arrow function inside like:

people.forEach(({firstName},{lastName}) => {
  console.log(firstName) || displayInPreview(firstName);
  console.log("lastName",lastName || displayInPreview(lastName));
})

But I get undefined for the lastName. Any ideas to display there other parameters? Thanks!

Hans Brough
Hans Brough
~ 4 years ago

I like the Skyler example at the end as it combines a few concepts - the downside of course is it assumes the order of objects in the array won't change. For that reason I'd just use .find() and then perhaps de-structure the response as needed.

~ 2 years ago

Hi Lluís Peinado, If you put lastName just next to firstName then it works.

people.forEach(({firstName, lastName}) => { console.log(firstName) || displayInPreview(firstName); console.log("lastName",lastName || displayInPreview(lastName)); })