All you need to know about destructuring in JavaScript
Tags: javascript, syntax, tips, learning
Published: 2018-10-08 13:30:43

---
title: All you need to know about destructuring in JavaScript
published: true
description: Destructuring syntax in JavaScript
cover_image: https://cdn-images-1.medium.com/max/1000/1*m3Mk3I2Cf5yoimlWA5OM6Q.png
tags: javascript, syntax, tips, learning
---

There is one fact that nobody can deny- everything , once built, can be destroyed. This concept also applies to programming. In this article, I will be discussing destructuring in JavaScript.

In JavaScript, destructuring is when you decompose the properties of an object or the indexes of an array to separate them to create specific variables. This does not mean that these separated objects or arrays can never be used again in the program.

Before we begin, however, I will quickly outline some important differences between arrays and objects. Arrays have number indexes and objects have string indexes. In addition, arrays and objects use different syntax.

![](https://cdn-images-1.medium.com/max/800/1*JmmIJw7Sp36pKyQ8JsgAUw.png)

In JavaScript, we can put objects in arrays and arrays in objects too, the syntax let us put everything where we want.

---

Now let's talk about the main subject, the destructuration. Destructing an object or an array doesn't mean that you will erase it from the program and can't ever use it again, it means that you'll get a specific part of it. Let's use examples, with Axios, a famous library. We can do an HTTP request (like with Fetch API or XmlHttpRequest) which returns us the following object's schema:

![](https://cdn-images-1.medium.com/max/800/1*60Jsu7-3_dUlS74MK5KJTw.png)

We'll stick to the data property! It's an object that contains all the server's response data. Let's say that we want to get all our users from our NodeJS API with MongoDB database, we may do something similar to the following code.

![](https://cdn-images-1.medium.com/max/800/1*vQR1NPR7ba0u7pNOvtYvAw.png)

In this case, the `req` constant will be an object with the same schema as seen before. So to get the users data, we has the `req.data` containing our array of users.

Now that we saw how to perform an Axios request, let's say that we only want one user that we can get from the route `/api/users/:userid`. For instance if the wanted user's id is `7`, we do a request to `/api/users/7`. But what if the API returns an array? Then we can do `req.data[0]` which can be a good way to do it but not as practical as if we used destructors…

First, let's get the `data` object. We can do `req.data` but we don't care about others req's properties so let's only get data. What we will do is an object destructuring assignment. (Finally, some excitment)

![](https://cdn-images-1.medium.com/max/800/1*mRLJVFae81GWKe4HN92e2g.png)

Yes, we only got the `data` property and it created an object named `data`! And you've done a "destructuring assignment" in JavaScript, great!

For instance we can destruct multiple properties in the same destructuring assignment, Axios provide a `status` property so let's get it by destructuration!

![](https://cdn-images-1.medium.com/max/800/1*CdBhBeLM15c95OT5wUSN1Q.png)

We can also give default values to any destructed property like bellow.

![](https://cdn-images-1.medium.com/max/800/1*Mqa0o0Rlg1suvJzefnnHtw.png)

Default value to a destructed propertyBut our object's name isn't what we really wanted, we want a `users` object to become easier to understand. So let's do a name's assignation without creating any more new variables.

![](https://cdn-images-1.medium.com/max/800/1*ivviQSrGSSYuVJ6bSDkCmA.png)

It's cool right? You can assign destructed properties to new variable's name in the same line and it doesn't look bad at all! So now we have our well named `users` object.

Still, `users` keep being an array, what could we do with it? I present you, the array's destructing assignment:

![](https://cdn-images-1.medium.com/max/800/1*4EzYu67iOKLLIye9DW82Ig.png)

Destructing assignment with an arrayIn this case, `a` is a new constant which will receive the index `0` of the array `[1, 2, 3]` (Which has the value `1`). The constant declaration's position in the destructing assignment define the selected index that will have it's value taken.

In this case, a is a new constant which will receive the index `0` of the array `[1, 2, 3]` (Which has the value `1`). The constant declaration's position in the destructing assignment define the selected index that will have its value taken.

Like for objects, we can have default values and multiple declarations. Their position always match the selected array's indexes.

![](https://cdn-images-1.medium.com/max/800/1*0YnMr2QWr6L6X3iBCWhmtg.png)

We can also bypass any indexes we want by putting a `,` without any variable declaration. In the following case we will bypass the indexes `0` and `1` (2 comas). `c` will be equal to the third index which have a value of `6`.

![](https://cdn-images-1.medium.com/max/800/1*K9OYIt3aqnVlJLBGSpQOIg.png)

Bypassing indexes in an array's destructing assignmentNow that we know how to use array's and object's destructing assignment, we finally could resolve our case of the variable `users` being an array with only one index.

We can use destructing assignments inside one another and it will works. So put an array's destructuring assignment in an object's destructuring assignment and you'll get the same result on the selected property:

![](https://cdn-images-1.medium.com/max/800/1*h-SEMV_yjAGiBpWCe39Yww.png)

To sum up, we've destructed the property `data` of the `axios.get('/api/users/7')` instruction's. Next we assigned data to a proper variable's name, `users`. After that we used the array's destructing assignment to define the variable `users` as the first index of the `data` property.

Every destructuring assignment can be used recursivly like seen previously, in any order you want. Object's destructuring assignment can be used in others object's destructuring assignment and same goes for array's destructuring assignment.

---

Now we perfectly know how to destructs objects and arrays in JavaScript. But there's another trick you can do with destructive assignments, the " rest pattern" or "spreading". This give you the possibility to take the remaining undestructed properties/indexes to a new variable. Here's a demonstration with arrays but you can do the same with objects :

![](https://cdn-images-1.medium.com/max/800/1*K-rA3NKjh0mz7cq9BmKvmQ.png)

---

Thanks you for reading, hope that now you know everything about destructuring in JavaScript!