
How to Merge Arrays Together in Javascript
There are a number of ways to merge arrays together in JavaScript. You can either use Array.concat
:
// Using Array.concat
const array1 = ['π', 'π'];
const array2 = ['π', 'π'];
array1.concat(array2);
Copied to clipboard!
You can also merge more than two arrays into one using concat
in the following way:
// Using Array.concat
const first = ['1οΈβ£', '2οΈβ£'];
const second = ['3οΈβ£', '4οΈβ£'];
const third = ['5οΈβ£', '6οΈβ£'];
// This will result in ['1οΈβ£', '2οΈβ£', '3οΈβ£', '4οΈβ£', '5οΈβ£', '6οΈβ£']
[].concat(first, second, third);
Copied to clipboard!
Keep in mind that both solution will return a new array instead of modifying the existing ones. Another way to combine arrays together, is by using destructuring:
// Using the destructuring assignment
const array1 = ['π', 'π'];
const array2 = ['π', 'π'];
[...array1, ...array2];
Copied to clipboard!
Again, this will create a new array instead of modifying the originals. You can also create a simple function to combine as many arrays as you would like:
function combine() {
const array = [];
[...arguments].forEach(arg => {
array.push(...arg);
});
return array;
}
combine([1]); // returns [1]
combine([1], [2]); // returns [1, 2]
combine([1], [2], [3]); // returns [1, 2, 3]
Copied to clipboard!
This works by making use of the arguments object, that is accessible inside functions.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.

Resources:
π More Webtips
Master the Art of Frontend
Unlimited access to hundred of tutorials
Access to exclusive interactive lessons
Remove ads to learn without distractions