Sometimes, we want to unpack array into separate variables in JavaScript.
In this article, we’ll look at how to unpack array into separate variables in JavaScript.
How to unpack array into separate variables in JavaScript?
To unpack array into separate variables in JavaScript, we use the array destructuring syntax.
For instance, we write
const [x, y] = ["foo", "bar"];
console.log(x);
console.log(y);
to assign 'foo'
to x
and 'bar'
to y
.
Conclusion
To unpack array into separate variables in JavaScript, we use the array destructuring syntax.