How do you destructure values out of an array?
Short answer
Array destructuring pulls values out by position into named variables using square-bracket syntax on the left side of an assignment.
Advertisement
AModel answer
Array destructuring pulls values out by position into named variables using square-bracket syntax on the left side of an assignment.
const [first, second] = [10, 20];
console.log(first, second); // 10 20Real-world example
Unpacking a [value, setValue] pair returned by React's useState hook.
Likely follow-ups
- How do you skip an element while destructuring an array?
Related topics