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.taylor2const shawn = names.shawn3const zayn = names.zayn4const halsey = names.halsey56console.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} = names23console.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']23const [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;34[a, b] = [b, a];5console.log(a); // 36console.log(b); // 1
- Ignoring some returned values
1function f() {2 return [1, 2, 3];3}45const [a, , b] = f();6console.log(a); // 17console.log(b); // 3
- Default values
1let a, b;23[a=5, b=7] = [1];4console.log(a); // 15console.log(b); // 7
- Create sub array with Rest parameters.
1const albums = ['Lover', 'Evermore', 'Red', 'Fearless']23const [, ...albums2] = albums45console.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: 203}45const statement = ({name, age}) => {6 return `My name is ${name}. I am ${age} years old.`7}89statement(anjan)
- Nested Object destructuring
1const profile: {2 name: 'Anjan',3 age: 20,4 professional: {5 profession: 'Full Stack Software Engineer',6 }7}89const {name, age, professional: {profession}} = profile1011console.log(professional) // error12console.log(profession) // Full Stack Software Engineer
- Default values
In any case, the property is undefined
1const {a = 10, b = 5} = {a: 3};23console.log(a); // 34console.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}1011const {12 name,13 age,14 address: { city },15 albums: [lover, ...rest],16} = taylor1718console.log(name) // Taylor Swift19console.log(age) // 3120console.log(city) // New York21console.log(lover) // Lover22console.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
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: