Destructuring Arrays
This Scrimba screencast was created by Dylan C. Israel.
Basic Usage
The destructuring array syntax is a shorthand for assigning array values to individual variables. When destructuring arrays, a set of square brackets ([ ]
) will surround the variables.
const colors = ['red', 'green', 'blue']
const [first, second, third] = colors
console.log(first) // "red"
console.log(second) // "green"
console.log(third) // "blue"
Default Values
When unpacking an array, variables can be assigned a default value in the event the array is undefined
.
const colors = ['red', 'green', 'blue']
const [first = 'white', second = 'white', third = 'white'] = colors
console.log(first) // "red"
console.log(second) // "green"
console.log(third) // "white"
See Also
This YouTube video was created by Steve Griffith.