DEV Community

Cover image for Everything you need to know about Javascript Destructuring
Anjan Shomooder
Anjan Shomooder

Posted on

Everything you need to know about Javascript Destructuring

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',
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

const taylor = names.taylor
const shawn = names.shawn
const zayn = names.zayn
const halsey = names.halsey

console.log(taylor)
console.log(shawn)
console.log(zayn)
console.log(halsey)
Enter fullscreen mode Exit fullscreen mode

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} = names

console.log(taylor)
console.log(shawn)
console.log(zayn)
console.log(halsey)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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); // 3
console.log(b); // 1
Enter fullscreen mode Exit fullscreen mode
  • Ignoring some returned values
function f() {
  return [1, 2, 3];
}

const [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
Enter fullscreen mode Exit fullscreen mode
  • Default values
let a, b;

[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7

Enter fullscreen mode Exit fullscreen mode
  • Create sub array with Rest parameters.
const albums = ['Lover', 'Evermore', 'Red', 'Fearless']

const [, ...albums2] = albums

console.log(albums2) // ['Evermore', 'Red', 'Fearless']
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
  • Nested Object destructuring
const profile: { 
       name: 'Anjan', 
       age: 20,
       professional: {
          profession: 'Full Stack Software Engineer',
       }
}

const {name, age, professional: {profession}} = profile

console.log(professional) // error
console.log(profession) // Full Stack Software Engineer
Enter fullscreen mode Exit fullscreen mode
  • Default values

In any case, the property is undefined

const {a = 10, b = 5} = {a: 3};

console.log(a); // 3
console.log(b); // 5
Enter fullscreen mode Exit fullscreen mode
  • 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],
} = taylor

console.log(name) // Taylor Swift
console.log(age) // 31
console.log(city) // New York
console.log(lover) // Lover
console.log(rest) // [ 'Evermore', 'Red', 'Fearless' ]
Enter fullscreen mode Exit fullscreen mode

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

By the way, this blog is originally published on cules coding website. . I would be glad if you give it a visit.

Shameless Plug

I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js.
If you are interested you can check the video.

You can also demo the application form here

Screenshot of Insta Carousel

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

If you have any questions, please comment down below.
You can reach out to me on social media as @thatanjan .
Stay safe. Goodbye.

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.

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.
Alt Text

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

Blogs you might want to read:

Videos might you might want to watch:




Discussion (0)