Menu

1 / 2

Array Destructuring Explained

DEV Community·Nikita Maharana·6 months ago
#MPT66BM5
Reading 0:00
15s threshold

Array Destructuring means extracting values from arrays and assigning in a convenient way for unpacking array elements into distinct variables.

or Simply, Array destructuring means taking values from an array and storing them into separate variables easily.

e.g:1

let fruits = ["apple", "grapes", "orange"];

let [first, second, third] = fruits;

console.log(first); //'apple'

console.log(second); //'grapes'

console.log(third); //'orange'

e.g:2

Accessing by their index values

let fruits = ["apple", "grapes", "orange"];

let first = fruits[0];

let second = fruits[1];

let third = fruits[2];

console.log(first); //'apple'

console.log(second); //'grapes'

console.log(third); //'orange'

The Rest Syntax: It allows you to capture the remaining elements of an array that have not been destructured into a new array.It's denoted by three dots (...).

e.g:

let fruits = ["apple", "banana", "orange", "grapes", "kiwi"];

let [first, second, ...rest] = fruits;

console.log(first); //'apple'

console.log(second);…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.