Categories
JavaScript Answers

How to Use the JavaScript Spread Operator to Concatenate Multiple Arrays?

Spread the love

Sometimes, we want to use the JavaScript spread operator to concatenate multiple arrays.

In this article, we’ll look at how to use the JavaScript spread operator to concatenate multiple arrays.

Use the JavaScript Spread Operator to Concatenate Multiple Arrays

To use the JavaScript spread operator to concatenate multiple arrays, we just spread all the entries from the existing arrays to a new array.

For instance, we write:

const fruits = ["apples", "bananas", "pears"];
const vegetables = ["corn", "potatoes", "carrots"];
const produce = [...fruits, ...vegetables];
console.log(produce);

to spread all the fruits and vegetables array entries to the produce array.

Therefore, produce is:

["apples", "bananas", "pears", "corn", "potatoes", "carrots"]

Conclusion

To use the JavaScript spread operator to concatenate multiple arrays, we just spread all the entries from the existing arrays to a new array.

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 *