How to Destructure an Array in JavaScript

featured Image

Javascript is a versatile and powerful programming language that is widely used in web -development. One of the most used features is its ability to manipulate arrays. And one of the most common ways of working with array in Javascript is using destructuring.

What is Destructuring?

Destructuring is a Javascript expression that allows you to extract values from arrays, or properties from objects, into distinct variables. It can be used to extract values of individual elements from an array or sub-array.

How to Destructure an Array

Destructuring an array is a simple process in Javascript. All we will need to do is define an array using the var or let keyword, followed by the name of the array and its elements.

let myArray = [1, 2, 3, 4, 5];

Once we have defined your array, we can use destructuring to extract individual elements and assign them to variables. This is done using the [] brackets and the variable name.

let [firstElement, secondElement, thirdElement] = myArray;
console.log(firstElement); // Output: 1
console.log(secondElement); // Output: 2
console.log(thirdElement); // Output: 3

In the example above, we have extracted the first three elements of the array and have assigned them to the variables firstElement, secondElement, and thirdElement. Using destructuring we can extract entire sub-arrays from an array.

let [firstElement, secondElement, ...remainingElement] = myArray;
console.log(firstElement); // Output: [1]
console.log(secondElement); // Output: [2]
console.log(remainingElement); // Output: [3, 4, 5]

In this example, we have used the ... operator ( spread oprator ) to extract the first two elements of the array and assign them to the firstElement and secondElement variable. The remaining elements of the array are then assigned to the remainingElement variable.

Destructuring in Function Parameters

We can also use destructuring in function parameters to make it easier to work with arrays and objects. When a function is called, the values passed to it are automatically destructured and assigned to the corresponding variables.

let myArray = [1, 2, 3, 4, 5];

function myFunction([first, second, third]) {
  console.log(first); // Output: 1
  console.log(second); // Output: 2
  console.log(third); // Output: 3
}

myFunction(myArray);

In the above example, we have defined a function that takes an array as a parameter. The first, second, and third elements of the array are automatically destructured and assigned to the first, second, and third variables.

Conclusion

Here, we have learned how to use destructing an array and how we can use destructuring on javascript function parameters to work with objects and arrays easily.

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *