The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Everything you need to know about Javascript Destructuring

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

In this blog, you will learn everything you need to know about javascript destructuring.

I have already created a video about it on my youtube channel. Check that out for more details.

If you like this video, please like share, and Subscribe to my channel.

Why does Destructuring exist in javascript?

Because it solves our problem. Let's see an example.

This is a normal object containing the names of 4 people.

1const names = {
2 taylor: 'Taylor Swift',
3 shawn: 'Shawn Mendes',
4 zayn: 'Zayn Malik',
5 halsey: 'Halsey',
6}

Now if I ask you to print all the file names manually to the console, how would you do that. Possibly like this:

1console.log(names.taylor)
2console.log(names.shawn)
3console.log(names.zayn)
4console.log(names.halsey)

It works. But it is so annoying to use this dot notation. But how can we make this better?

1const taylor = names.taylor
2const shawn = names.shawn
3const zayn = names.zayn
4const halsey = names.halsey
5
6console.log(taylor)
7console.log(shawn)
8console.log(zayn)
9console.log(halsey)

Much better. But we are still repeating the same task. What if we can declare and assign the object properties on variables on a single?

It would be better right?. This is where Object destructuring helps us. So instead we can do something like this :

1const { taylor, shawn, zayn, halsey} = names
2
3console.log(taylor)
4console.log(shawn)
5console.log(zayn)
6console.log(halsey)

Wow! That is way better than before.

But, how is it working?

It is so simple. We are just pulling out the properties from the object and storing them in a variable. And the variable name will be the same as the property name by default. You can change that like this.

1const { taylor, shawn, zayn: zaynMalik, halsey} = names

What about array destructuring?

Array destructuring works in a similar way with some differences. We know data are stored in an array with indexes. They are sequential. So, while destructuring you have to maintain the order. For example:

1const albums = ['Lover', 'Evermore', 'Red', 'Fearless']
2
3const [lover, ever] = arr

And also arrays don't have a property for values. So, you can just give whatever variable name you want.

Let's see some use cases of Object destructuring and array destructuring.

Array destrucutring

  • Swapping variables

1let a = 1;
2let b = 3;
3
4[a, b] = [b, a];
5console.log(a); // 3
6console.log(b); // 1
  • Ignoring some returned values
1function f() {
2 return [1, 2, 3];
3}
4
5const [a, , b] = f();
6console.log(a); // 1
7console.log(b); // 3
  • Default values
1let a, b;
2
3[a=5, b=7] = [1];
4console.log(a); // 1
5console.log(b); // 7
  • Create sub array with Rest parameters.
1const albums = ['Lover', 'Evermore', 'Red', 'Fearless']
2
3const [, ...albums2] = albums
4
5console.log(albums2) // ['Evermore', 'Red', 'Fearless']

Object destrucutring

  • Unpacking fields from objects passed as a function parameter

It is really useful for react props.

1const anjan = {
2 name: 'Anjan', age: 20
3}
4
5const statement = ({name, age}) => {
6 return `My name is ${name}. I am ${age} years old.`
7}
8
9statement(anjan)
  • Nested Object destructuring
1const profile: {
2 name: 'Anjan',
3 age: 20,
4 professional: {
5 profession: 'Full Stack Software Engineer',
6 }
7}
8
9const {name, age, professional: {profession}} = profile
10
11console.log(professional) // error
12console.log(profession) // Full Stack Software Engineer
  • Default values

In any case, the property is undefined

1const {a = 10, b = 5} = {a: 3};
2
3console.log(a); // 3
4console.log(b); // 5
  • Nested object and array destructuring

1const taylor = {
2 name: 'Taylor Swift',
3 age: 31,
4 address: {
5 city: 'New York',
6 country: 'USA',
7 },
8 albums: ['Lover', 'Evermore', 'Red', 'Fearless'],
9}
10
11const {
12 name,
13 age,
14 address: { city },
15 albums: [lover, ...rest],
16} = taylor
17
18console.log(name) // Taylor Swift
19console.log(age) // 31
20console.log(city) // New York
21console.log(lover) // Lover
22console.log(rest) // [ 'Evermore', 'Red', 'Fearless' ]

That's all you need to know about javascript destructuring.

Shameless Plug

I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostGenerate random numbers in JavaScript
Next PostEverything you need to know about template strings