Javascript: Merging multiple arrays with spread syntax
( 27 Articles)

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:
- Calculate Variance and Standard Deviation in Javascript
- Javascript: Capitalize the First Letter of Each Word (Title Case)
- 2 Ways to Set Default Time Zone in Node.js
- [JS] Generate a random number between Min and Max and exclude a specific number
- Javascript: Looping through Keys/Values of an Object
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
0 Comments