Destructuring Objects and Arrays in ES6
In this article, we will learn about destructuring objects and arrays in ES6. We will also have a look into the JavaScript REST operator.
So, let's give it a go. To follow along with the code snippets, you can use an online editor. One of the interesting new features introduced in ES6 is destructuring. It provides a way to break up objects and arrays into variables using a very concise syntax. Let's take a look into the below-given code snippet
- const studentDetails = {
- name : 'Joey',
- age : 26,
- country : 'USA'
- }
We have a Student Details object, and if we want to access the studentDetails.Name property. We need to write code like this.
- console.log(studentDetails.name);
What if we want to use this property often in our code? Then we should keep it in a variable instead of using the studentDetails object every time.
- const name = studentDetails.name;
- console.log(name);
Now, our code can be a bit shorter every time we use the variable name, but we still have to manually write the code to declare the variables and extract the properties.
Destructuring provides an incisive way to break out an object's properties into separate parts. Let's see in practice
- const {name} = studentDetails;
ES6 will declare a constant called "name" and assign the value of the studentDetails.name to it.
If we want to extract more properties of studentDetails, then we can just provide them and set the curly braces separated by commas, name, age, and country.
- const {name,age,country} = studentDetails;
- console.log(name + ' '+age + ' '+country); // will print Joey 26 USA
We can also provide alias name to variable while extracting an object's property.
- const {name: StudentName,age : StudentAge, country : StudentCountry} = studentDetails;
- console.log(StudentName + ' '+StudentAge);
As you can see we have added alias name after the colon character. Beware though that since it uses a colon this looks a bit like a TypeScript type declaration. Inside a destructuring block, the colon has a different meaning, and it's only used to alias property names.
Let's take another example with undefined properties and passing default values to them,
- const studentDetails = {
- name : 'Joey',
- age : undefined,
- country : 'USA'
- }
As you can see, we on purpose declared age property as undefined to see the default value for destructing. If you'd prefer a value like 30 if age is undefined then we need to write code like this
- const {name: StudentName,age : StudentAge = 30, country : StudentCountry} = studentDetails;
When using destructuring, default values are only applied if the property is undefined, null will stay as null. As you can see we have assigned a default value for age to 30.
ES6 destructing works with more than just objects, it works with arrays too
Let's have some fun destructuring with Arrays using a new JavaScript operator rest.
Let's say we have an array of Student Names like Monika, Charlie, and Sheldon. If we want to assign the first name in this array to a variable called firstStudentName, then we can type in a traditional way.
- const studentNames = ["Monika", "Charlie", "Sheldon"];
- const firstStudentName = names[0];
Now let's see how we can use destructuring instead.
As you remember from earlier examples with objects, that when we were destructuring objects, we surrounded our destructuring assignment with curly braces. Curly braces are often used with objects in JavaScript.
The characters most often used with arrays are square brackets, and we use those to destructure an array.

Ajay MorPosted Jul 19, 2019, 2:50 AM
https://www.c-sharpcorner.com/article/ecmascript-6-new-featu-overview-comparison-var-vs-let/
Ajay MorPosted Jul 19, 2019, 2:50 AM
Https://www.c-sharpcorner.com/article/es6-arrow-functions/
Ajay MorPosted Jul 19, 2019, 2:46 AM
I appreciate your advice on linking other articles in this series. So, everyone please head over to the links shared by me in above made comments to get hold of my other articles in this series.
Mahesh ChandPosted Jul 17, 2019, 3:14 PM
Thank you, Ajay. Please link your previous and next articles in this series.