Back to Basics - Simple Destructuring in JavaScript
Tags: beginners, javascript, womewhocode
Published: 2019-08-22 21:00:29

---
title: Back to Basics - Simple Destructuring in JavaScript
published: true
description: easily destructure object and arrays
tags: #beginner, #javascript, #womewhocode
---

Want to type less, keep your code concise, and quickly access values within objects and arrays? Then you've come to the right place. This week's Back to Basics series will review the incredible (and perhaps underrated?) feature of destructuring in JavaScript. I'll show how to 'take a peak' inside data structures like objects and arrays and assign specific values to a variable for quick easy reference. 

As implied by the name, destructuring can help simplify complex data structures into smaller and more manageable fragments. This can be especially helpful when passing objects/arrays as arguments, or if you want to frequently access a deeply nested value within an object since you can save yourself some typing (and potentially typos) by assigning the value to a variable.

Let's begin with simple examples of destructuring arrays and objects in JavaScript. 

Below you can see an example with an array of princesses 👑. For the sake of simplicity we are going to destructure the array and create easy references to the first two princesses in the array - let's call them `first` and `second`. On line 34 we use the array literal and assign it to our original array. Inside of the array literal we list our new variables `first` and `second` which will be mapped to the first two elements in the princesses array in their corresponding order. You can see the logs on lines 35 and 36. 

Another option is to assign the remainder of the array to a variable. This can be accomplished by using the spread operator as the third and final variable within our array literal (`...remaining`). You can see the logs from this alternative example on lines 40-42.

![simple array destruction example](https://thepracticaldev.s3.amazonaws.com/i/bp49dyicoq9ghr82x8j4.png)<figcaption>two variations of destructuring a simple array</figcaption>

Moving on now..let's explore a simple object example. We need to keep track of the hometowns of each princess (very important stuff, I know).  We have a rather large object which lists each princess, her rumored country of origin, and the region said country belongs to. While this is just a fraction of princesses, we can imagine that a complete list would create a much larger object. Hypothetically speaking throughout our program we will want to reference the object which represents each region. We can easily destructure our hometowns object into variables which represent each region. 

On line 58 we use the object literal to create a variable `europe` and assign it to the object `hometowns`. This variable will be mapped to the value of the key `europe` inside the hometowns object as proved on line 60. We can also declare two variables like `northAmerica` and `asia` in one object literal to pull of the remaining two nested object - take a look at lines 62-66!

![simple object destructuring example](https://thepracticaldev.s3.amazonaws.com/i/7fdg2r05u0wlysqqpj2t.png)<figcaption>you can assign multiple variables at once</figcaption>

Lastly, let's take a brief look at how you can destructure a function parameter.  From our princess data - you'd really like to print out the total number of countries represented from each region. To achieve this efficiently through destructuring, pass in the larger hometown object and pick off each region. You can then log the count of keys inside of each country object. Note that here we destructured each region, but you could also just destructure `{asia}` similar to the earlier object example. 

![destructuring function parameter example](https://thepracticaldev.s3.amazonaws.com/i/nqs11bol3d94atiufx0a.png)<figcaption>hometown object is destructured as function parameter</figcaption>

I hope this post helped to provide some foundation for destructuring in JavaScript.  There are a lot of other cool ways you can utilize destructuring within your code and I encourage you to explore further. I've linked the [MDN doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) for your convenience! 

![salute princess gif](https://media.giphy.com/media/11cwuBQ4TLa8FO/giphy.gif)