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.