DEV Community

Nikita Maharana
Nikita Maharana

Posted on

Array Destructuring Explained

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); //'banana'
console.log(rest); //['orange', 'grapes', 'kiwi']

Top comments (0)