Skip to content
loading...

Destructuring Array in javascript

github logo ・1 min read  

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.

twitter logo DISCUSS
Classic DEV Post from Feb 3

What would you teach a frontend beginner in 2020?

In April, I will start teaching frontend development to design students at university. The course cu...

K
Muhammad Awais profile image
ReactJS | Angular | JavaScript | Frontend | Ruby on Rails | User Interface | Ionic | Heroku | AWS