ES6: Destructuring

ES6-destructuringThis is the first post in a series I’ll be doing about new ES6 features.  The goal is not to merely explain the concepts, but to also show “real-world” – or real enough – applications of the concepts so that you can understand why and when you might use them.  Hopefully you will be able to start recognizing scenarios or areas in your codebase that could benefit from these new features. After all, new language features should be used to not only help us write cleaner, more expressive code, they should also help us or even challenge us to think about the way we solve problems.

The first topic feature that will be covered is destructuring.

What?

Destructuring is a way of breaking down a data structure (de-structuring) into smaller parts.  ES6 adds this feature for use with arrays and objects.

Why?

Destructuring provides a cleaner, less verbose way of extracting values from objects and arrays.  Rather than having to write

or in the case of an array, explicitly specify the index for the value you’re trying to get, you can instead write

There are several other selling points, but before we dive into those, let’s see some code.

How?

Objects

Let’s start with objects.  The syntax for destructuring follows the same syntax as an object literal itself, a block statement.  Consider the code below:

Destructuring can be done in one of two ways:

The result of this is three variables – ‘color’, ‘type’, and ‘name’, all with the value of their respective property values.  It should be noted here that all of the three variable types – var, let, const – need an initializer (the object or array to the right of the assignment operator (=)).  As a side note, while var and let do not need to be initialized for non-destructured assigments, const always needs to be initialized, regardless of whether it’s a destructured value or not.

If we print out these values, the result will be as below:

As you’re hopefully already starting to see, the same syntax that is used to construct data can now be used to extract data.

Important to note here is that we actually aren’t changing the object itself, which is why node.type still returns “Test” although we assigned the variable value to “Homework”.  Destructuring doesn’t modify the source, whether it is var, let or const.  Only the destructured variables (if they’re var or let) are modified.

Assigning destructured variable to a different name

What if you don’t want to use the property name as the variable name? You can change it like so:

Side note: what happens if the object property or object name is wrong?  It will throw a ReferenceError:

Nested objects

Destructuring is also applicable to nested objects, like below:

Arrays

Array destructuring is much like object destructuring, with the main difference being that you don’t specify the index number.

We can skip values in the array by leaving them blank. As you can see, thrName is an arbitrary name, in this case referring to the third position in the array.

Nested arrays

Just like with nested objects, so too can nested arrays be destructured:

Mixed data structures

Lastly, it is possible to apply what we’ve learned above in order to destructure mixed data structures, like below:

Side notes

Sometimes you will see the object or array literal to the right of the destructuring statement or expression:

With arrays, you can use the rest operator (another ES6 feature) to iterate through the values without having to explicitly call them out:

Default values can be assigned if the object property or array value does not yet exist:

When?

If you’re looking to convert some of your ES5 code to ES6, or just want to be aware of use cases for this new feature as you’re developing a current or future application, the following will be patterns to keep an eye out for.

As mentioned in the beginning of this post, a big selling point for destructuring is its cleaner way of extracting data from a data structure, instead of having to write something verbose like let val = someObject.someProperty.maybeSomeNestedProperty or something repetitive like

Another great use case is swapping values. Traditionally, developers have had to make use of a temp variable in order to swap values between variables, but now we can do this:

Destructuring can be used with arrays and objects returned from a function, too:

That’s it for this week’s post. It’s sometimes difficult to read code interspersed with text, so I’ll put the code on GitHub.

I have a whole backlog of topics for future posts, and I’m not sure if the next one will be on ES6 or not. If you find this post useful, would like more clarifications on the concept, or – most importantly – would like to better understand why you might use this feature, please comment down below.

 

coreyc

 

One thought on “ES6: Destructuring

Leave a Reply

Your email address will not be published. Required fields are marked *