Destructuring arrays and destructuring objects are similar. We use square brackets ([]) instead of curly brackets ({}).
let [one, two] = [1, 2, 3, 4, 5]
console.log(one) // 1
console.log(two) // 2
while destructure an array, your first object belongs to 1st item from the array, the second object belongs to 2nd object from the array, so on and so far.
let scores = ['98', '95', '93', '90', '87', '85']
let [first, second, third, ...rest] = scores
console.log(first) // 98
console.log(second) // 95
console.log(third) // 93
console.log(rest) // [90, 87, 85]
if you want to get only a few items from the first indexes and used the rest of them in the same manner, so here comes the Rest Operator in usage like above.
Thankyou.
Top comments (0)