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.

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:


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

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

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); // 3
console.log(b); // 1
  • Ignoring some returned values
function f() {
return [1, 2, 3];
const [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
  • Default values
let a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
  • Create sub array with Rest parameters.
const albums = ['Lover', 'Evermore', 'Red', 'Fearless']
const [, ...albums2] = albums
console.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.`
  • 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
  • Default values

In any case, the property is undefined

const {a = 10, b = 5} = {a: 3};
console.log(a); // 3
console.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 {
address: { city },
albums: [lover,],
} = 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' ]

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

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, feel free to contact me on any 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.



More screenshots

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.


Blogs you might want to read:

Videos might you might want to watch:

Next PostEverything you need to know about template strings