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:

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.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)

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)

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.`
}
statement(anjan)
  • 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 {
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' ]

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

Shameless Plug

Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.

Lessons

Demo

You can demo the website from here

Features

  • Static Blog pages will make the website load faster.
  • Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
  • Autocomplete search feature for the blog posts.
  • Real-time view counter and so on.

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. I am available on Upwork

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