Javascript ES6: Merge n arrays in an efficient way.

Rhea RB
1 min readNov 1, 2022

--

Arrays are everywhere. One cannot code without learning arrays. There comes a point where we have to merge several arrays and the question lies how ?
Merging has to be efficient, scalable and easy.
Well, javascript has got you covered.

Merging Arrays

Lets say you have two arrays to merge,

let a = [1,2,3]
let b = [4,5,6]
We can use the concat() function and merge them, a = a.concat(b);

console.log(a);
a = [1,2,3,4,5,6]

Now, lets say we do not know how many arrays we have to merge, it can be 3 or 4, can have many elements.

In such a scenario using the spread operator (…) makes a huge difference.

let a = [1,2,3]
let b = [4,5,6]
let c = [7,8,9]
using spread operatorlet result = [..a,..b,...c]

console.log(result);
result = [1,2,3,4,5,6,7,8,9]

So easy and simple.

Make sure to use to use it on your code when you you need to merge arrays.

Thank you for reading !

--

--

Rhea RB

Hello ! I am a senior software engineer, passionate about tech and product.