Categories
JavaScript Answers

How to unpack array into separate variables in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *