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.
const names = {taylor: 'Taylor Swift',shawn: 'Shawn Mendes',zayn: 'Zayn Malik',halsey: 'Halsey',}
Now if I ask you to print all the file names manually to the console, how would you do that. Possibly like this:
console.log(names.taylor)console.log(names.shawn)console.log(names.zayn)console.log(names.halsey)
It works. But it is so annoying to use this dot notation. But how can we make this better?
const taylor = names.taylorconst shawn = names.shawnconst zayn = names.zaynconst halsey = names.halseyconsole.log(taylor)console.log(shawn)console.log(zayn)console.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 :
const { taylor, shawn, zayn, halsey} = namesconsole.log(taylor)console.log(shawn)console.log(zayn)console.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.
const { 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:
const albums = ['Lover', 'Evermore', 'Red', 'Fearless']const [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
let a = 1;let b = 3;[a, b] = [b, a];console.log(a); // 3console.log(b); // 1
- Ignoring some returned values
function f() {return [1, 2, 3];}const [a, , b] = f();console.log(a); // 1console.log(b); // 3
- Default values
let a, b;[a=5, b=7] = [1];console.log(a); // 1console.log(b); // 7
- Create sub array with Rest parameters.
const albums = ['Lover', 'Evermore', 'Red', 'Fearless']const [, ...albums2] = albumsconsole.log(albums2) // ['Evermore', 'Red', 'Fearless']
Object destrucutring
- Unpacking fields from objects passed as a function parameter
It is really useful for react props.
const anjan = {name: 'Anjan', age: 20}const statement = ({name, age}) => {return `My name is ${name}. I am ${age} years old.`}statement(anjan)
- Nested Object destructuring
const profile: {name: 'Anjan',age: 20,professional: {profession: 'Full Stack Software Engineer',}}const {name, age, professional: {profession}} = profileconsole.log(professional) // errorconsole.log(profession) // Full Stack Software Engineer
- Default values
In any case, the property is undefined
const {a = 10, b = 5} = {a: 3};console.log(a); // 3console.log(b); // 5
- Nested object and array destructuring
const taylor = {name: 'Taylor Swift',age: 31,address: {city: 'New York',country: 'USA',},albums: ['Lover', 'Evermore', 'Red', 'Fearless'],}const {name,age,address: { city },albums: [lover, ...rest],} = taylorconsole.log(name) // Taylor Swiftconsole.log(age) // 31console.log(city) // New Yorkconsole.log(lover) // Loverconsole.log(rest) // [ 'Evermore', 'Red', 'Fearless' ]
That's all you need to know about javascript destructuring.
Shameless Plug
I have made few project based videos with vanilla HTML, CSS, and JavaScript.
You will learn about:
- Javascript intersection observer to add cool effects
- DOM manipulation
- Aligning elements with CSS positions.
- How to make responsive websites.
- How to create slide based webpage.
These will be great projects to brush up on your front end skills.
If you are interested you can check the videos.
You can also demo the application from here:
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project.
About me
Why do I do what I do?
The Internet has revolutionized our life. I want to make the internet more beautiful and useful.
What do I do?
I ended up being a full-stack software engineer.
What can I do?
I can develop complex full-stack web applications like social media applications or e-commerce sites. See more of my work from here
What have I done?
I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.
Screenshot
I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.
Want to work with me?
I am looking for a team where I can show my ambition and passion and produce great value for them. Contact me through my email or any social media as @thatanjan. I would be happy to have a touch with you.
Contacts
- Email: thatanjan@gmail.com
- linkedin: @thatanjan
- portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Instagram (youtube channel): @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: