Javascript: Merging multiple arrays with spread syntax

Last updated on February 13, 2022 A Goodman Loading... Post a comment

In order to merge (or concatenate) two, three, or more arrays in ES6, we can use the spread syntax.

Example:

const arrA = [1, 2, 3, 4, 5]
const arrB = [6, 7, 8]
const arrC = [9, 10]

// merging
const arrAll = [...arrA, ...arrB, ...arrC]

// printing the result
console.log(arrAll)

Output:

[1, 2, 3, 4,  5, 6, 7, 8, 9, 10]

Note:

  • Using the spread syntax does not change the existing arrays. It always returns a new array
  • You can use any number of arrays, even only one.

Further reading:

You can also check out our Javascript category page, TypeScript category page, Node.js category page, and React category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles