Destructure anything that returns an array

October 13, 2019

ES6 came with plenty of cool features. Destructuring is one of them. It became so easy to unpack values from arrays, or properties from objects and assign them to a specific variable.

Previously, we had to do this : πŸ‘Ž

var stack = ["JavaScript", "React", "Redux"]
var language = stack[0] // JavaScript
var library = stack[1] // React
var stateManagement = stack[2] // Redux

But with ES6 we can easily destrucutre them like this : πŸ‘Œ

const stack = ["JavaScript", "React", "Redux"]
const [language, library, stateManagement] = stack

Now, we can leverage this to destructure anything that returns an array : 🀟

const stack = "JavaScript-React-Redux"
const [language, library, stateManagement] = stack.split("-")