Destructuring assignment is a nice feature that comes with ES6. Destructuring is a JavaScript expression that makes it possible to unpack values from an array or properties from an object into separate variables. That is, we can extract data from arrays and objects and assign it to variables. Why is this necessary?
Imagine if we want to extract data from an array. First, how will this be done?
var introduction = ["Hello", "I" , "am", "John"];
var greeting = introduction[0];
var name = introduction[3];
console.log(greeting);//"Hello"
console.log(name);//"John"
We can see that when we wanted to extract the data from the array, we had to do the same thing over and over again. ES6 destructuring assignments make it easy to extract this data. How is this? This article discusses array destructuring assignments. My next article will discuss things. Let’s get started.
Basic Destructuring
If we want to extract data using arrays, it’s quite simple to use the destructuring assignment. Let’s refer to our first example for arrays. Instead of going through that repetitive process, we’ll do this.
var introduction = ["Hello", "I" , "am", "John"];
var [greeting, pronoun] = introduction;
console.log(greeting);//"Hello"
console.log(pronoun);//"I"
We can also do this with the same result.
var [greeting, pronoun] = ["Hello", "I" , "am", "John"];
console.log(greeting);//"Hello"
console.log(pronoun);//"I"
Declaring Variables before Assignment The variables can be declared before being assigned like this.
var greeting, pronoun;
[greeting, pronoun] = ["Hello", "I" , "am", "John"];
console.log(greeting);//"Hello"
console.log(pronoun);//"I"
Notice that the variables are set from left to right. So the first variable gets the first item in the array, the second variable gets the second variable in the array, and so on.
Skipping Items in an Array
What if we want to get the first and last item on our array instead of the first and second item and we want to assign only two variables? This can also be done. Look at the example below.
var [greeting,,,name] = ["Hello", "I" , "am", "John"];
console.log(greeting);//"Hello"
console.log(name);//"John"
What just happened? Look at the array on the left side of the variable assignment. Notice that instead of having just one comma, we had three. The comma separator is used to skip values in an array. So if you want to skip an item on an array, just use a comma.
Let’s do another one. I think it’s fun. Let’s skip the first and third items on the list. How will we do this?
var [,pronoun,,name] = ["Hello", "I" , "am", "John"];
console.log(pronoun);//"I"
console.log(name);//"John"
So the comma separator does the magic. So if we want to skip all items, we just do this.
var [,,,,] = ["Hello", "I" , "am", "John"];
Assigning the rest of an array
What if we want to assign some of the array to variables and the rest of the items on an array to a particular variable? We’ll do this.
var [greeting,...intro] = ["Hello", "I" , "am", "John"];
console.log(greeting);//"Hello"
console.log(intro);//["I", "am", "John"]
Using this pattern, you can unpack and assign the remaining part of an array to a variable.
Destructuring Assignment with Functions We can also extract data from an array returned from a function. Let’s say we have a function that returns an array like the example below.
function getArray() {
return ["Hello", "I" , "am", "John"];
}
var[greeting,pronoun] = getArray();
console.log(greeting);//"Hello"
console.log(pronoun);//"I"
We get the same results.
Using Default Values Default values can be assigned to the variables just in case the value extracted from the array is undefined
.
var[greeting = "hi",name = "John"] = ["hello"];
console.log(greeting);//"Hello"
console.log(name);//"John"
So name
falls back to “John” because it is not defined in the array.
Swapping Values using Destructuring Assignment One more thing. We can use destructuring assignment to swap the values of variables.
var a = 3;
var b = 6;
[a,b] = [b,a];
console.log(a);//6
console.log(b);//3
Next, I’ll write on Object Destructuring.
Any questions or addition? Please leave a comment.
Thank you for reading 🙂