DEV Community

emertola
emertola

Posted on • Updated on

Random Front-end codes I learned today (#001) - Array destructure

Without further intro, I'll go straight to the point.

What is Array destructure?

As MDN describes it, destructuring is a way of unpacking values from, in our case, an Array (or properties from an Object, but we'll focus on the Array on this topic).

There are a lot times, when I want to access a value from an array, say the first value, and store it in a variable, this is what I normally do:
Alt Text

With array destructuring, I discovered that I can do that in just a single line of code.
Alt Text

This means that the first value of the array (which is pencil) will immediately be stored to a variable pencil. It could also be another variable name, and it'll still be of the same result:
Alt Text

Top comments (0)