Using Destructuring in JS to Write Cleaner Code
Tags: javascript, beginners, productivity, tutorial
Published: 2022-11-17 22:42:39

As a beginner in vanilla JavaScript, I still struggle with writing clean, concise and easy to read code (otherwise known as DRY code). So, I get excited when I learn a method that makes my code look more ✨elegant✨. Today, we will go over how destructuring does just that. 

---


## How I initially learned...

Normally, if we wanted to target a specific element in an array we would use the indices of the array. Below we have an array with the first 6 letters of the alphabet. To target the letter A and assign to it its own local variable, we set the variable equal to the letter's index in the array as shown below.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd5yauuewil2qt9vul0i.png)

When we console.log the newly made variable we get back the letter A in the original array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/py07lafdpxeoqliarbu3.png)

As for objects, we would use dot notation to set new variables to specific elements within the object. Like so...


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n5xq9jvvvb4x60lukv7.png)


And when we console.log the new variable it will show the value for the key muppetName... 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqcw347a6986te8nmwc5.png)

Now this seems all fine and dandy but what if we had much larger arrays or objects and we wanted to set each element or value to it's own local variable? That would make for a lot of lines of code that is not so easy to read. That's where the wonderful tool of destructuring comes in and cleans everything up. 

---


## What is destructuring? 

The destructuring assignment syntax was introduced in ES6 and it allows you to take objects and arrays and easily extract the values and assign them to their own variables. 

## Destructuring arrays

Below we have the same array as before but now we can set each value of the array to its own variable in one line of code. To break this down, the new variables need to equal the index positions of the original array and essentially inserted into a new array. Then the new array must be set equal to the original array variable. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzbecfk3ja09ucutpz4a.png)

And when we console.log each individual variable the values of the original array will show. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz28xe1qsdo1d13onfcd.png)



We can also skip values within arrays by taking out value we want to skip and keeping the comma. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nh58fqaefhpx9gt0pct.png)

This will return


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvhw7lzy11fuimgabkca.png)

Another pretty useful trick is the ability to copy the rest of the array and put it into a new array using the spread operator. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zetl6tg9andm52g8ole.png)

Adding the spread operator syntax to the array constant then returns 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dudz23mjx43zz1ov45c0.png)


Functions that return arrays can also be destructured as show below


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a284f1ixcfs1bmgjarr6.png)

Same concept except the array constant is set to the function call with whatever parameters you want to pass and it will return the added and subtracted values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snb8gjfoiqd8o503hpoq.png)

As you can see this functionality works well on arrays but the power of it is really shown when it is used to destructure objects. 

---

## Destructuring objects

Unpacking objects works in basically the same way as destructuring an array except instead of square brackets we use curly braces and the variables we designate are not based on the index position but on the name of the key in the object. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5s4xz040opjoscpeni9.png)

Since the variables designated in the curly braces match the keys in the object it will return the values of the nestedName and nestedColor...


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png)

However, in the instance that we don't want the variable name to match the key name we can easily change it like so

 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7q36yvdl5a0ss57oz8g.png)

And when we console.log the new variable name it will console the same value.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png)

We can also access the nested key value pairs within the object we are working with and directly assign variables to them as well. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8dx3blgzcfnxd01ep35.png)

We set the new curly braced variables equal to the nested object we wanted to access and when we logged the variables it returned the values we wanted!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaas0m8t560y002rdw3a.png)


---

## To wrap things up

With all that said, here is a good visual representation of how destructuring cleans up your code when you need to assign local variables to each key value pair in an object...


**Dot notation:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6xfqrxzwx84bm1zwvg.png)


**Destructuring:** 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yosjcdtxiavogzsbkig6.png)


We went from seven lines of code to three! There are a few more cool destructuring tricks we can use to our advantage but these are just a couple of the basic things that I learned that I will definitely be using in my code from now on!