Javascript Array Destructuring
How to do the Array Destructuring?
Destructuring is the way to unpack the values from the object and array and use them without facing the complex code every time . It is used to extract values instead of pointing to each and every value every time we use them.
Let's first look at the array destructuring,
Why we need array destructuring?
Array destructuring is used in many ways and first look at the complex method of extracting the values so that we can compare how destructuring makes our lives much better.
So, this is the way we used before destructuring and ES6 module . Now let's see the simpler way of extracting the values.
Basic Array Destructuring
This is the Basic destructuring where in array we are putting out the values in the continuous manner with any given name just like the variables and using them anywhere.
Declaring variables before Assignment
We can also declare the variables before assigning into the array values .
Skipping Items into the array
We can also skip the items from the array , as before we can only access in the continuous manner when we extract the values from the array but we can access the value by skipping them with the comma separated ( , , ) .
Assigning the rest of an array
We can assign the rest values of an array with the spread operator ,and the variable which is assigned the rest values contains the array of values .
We can also provide the default value to the array just in case it will not throw an undefined value, if the array contains only one value and we try to access the second value , we will provide the default value as a fallback value of the variable
One more thing , is we can swap the values with the array destructuring as it assigns the values to another and mutates the value of the variable.
So, you can see how many ways we can use array destructuring and how it can makes our lives so much better. Practice the code , play with the destructuring concept and implement it in the live projects.
Thank you for reading :)
Comments
Post a Comment