Destructuring Assignment in ES6- Arrays

sarah_chima

Sarah Chima

Posted on October 30, 2017

Destructuring Assignment in ES6- Arrays

Destructuring assignment is a cool feature that came along with ES6. Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. That is, we can extract data from arrays and objects and assign them to variables. Why is this necessary?

Imagine if we want extract a data from an array. Previously, how will this be done?


    var introduction = ["Hello", "I" , "am", "Sarah"];
    var greeting = introduction[0];
    var name = introduction[3];

    console.log(greeting);//"Hello"
    console.log(name);//"Sarah"
Enter fullscreen mode Exit fullscreen mode

We can see that when we want to extract data from an array , we had to do the same thing over and over again. ES6 destucturing assignment makes it easier to extract this data. How is this so? This article discusses destructuring assignment of arrays. My next article will discuss that of objects. Let's get started.

Basic Destructuring

If we want to extract data using arrays, it's quite simple using 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", "Sarah"];
    var [greeting, pronoun] = introduction;

    console.log(greeting);//"Hello"
    console.log(pronoun);//"I"

Enter fullscreen mode Exit fullscreen mode

We can also do this with the same result.

    var [greeting, pronoun] = ["Hello", "I" , "am", "Sarah"];

    console.log(greeting);//"Hello"
    console.log(pronoun);//"I"

Enter fullscreen mode Exit fullscreen mode

Declaring Variables before Assingment
The variables can be declared before being assigned like this.


    var greeting, pronoun;
    [greeting, pronoun] = ["Hello", "I" , "am", "Sarah"];

    console.log(greeting);//"Hello"
    console.log(pronoun);//"I"

Enter fullscreen mode Exit fullscreen mode

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", "Sarah"];

    console.log(greeting);//"Hello"
    console.log(name);//"Sarah"

Enter fullscreen mode Exit fullscreen mode

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 item on the list. How will we do this?

    var [,pronoun,,name] = ["Hello", "I" , "am", "Sarah"];

    console.log(pronoun);//"I"
    console.log(name);//"Sarah"

Enter fullscreen mode Exit fullscreen mode

So the comma separator does the magic. So if we want to skip all items, we just do this.

    var [,,,,] = ["Hello", "I" , "am", "Sarah"];

Enter fullscreen mode Exit fullscreen mode

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", "Sarah"];

    console.log(greeting);//"Hello"
    console.log(intro);//["I", "am", "Sarah"]

Enter fullscreen mode Exit fullscreen mode

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", "Sarah"];
    } 
    var[greeting,pronoun] = getArray();

    console.log(greeting);//"Hello"
    console.log(pronoun);//"I"
Enter fullscreen mode Exit fullscreen mode

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 = "Sarah"] = ["hello"];

    console.log(greeting);//"Hello"
    console.log(name);//"Sarah"
Enter fullscreen mode Exit fullscreen mode

So name falls back to "Sarah" 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
Enter fullscreen mode Exit fullscreen mode

Next, I'll write on Object Destructuring.

Any question or addition? Please leave a comment.

Thank you for reading :)

💖 💪 🙅 🚩
sarah_chima
Sarah Chima

Posted on October 30, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related