EN
JavaScript - concatenate multiple arrays
3
points
In this article, we would like to show you how to concatenate multiple arrays in JavaScript.
1. Using concat() method
In this example, we use concat() method to concatenate multiple arrays.
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b'];
const array2 = ['c', 'd'];
const array3 = ['e', 'f'];
const array4 = ['g', 'h'];
const resultArray = array1.concat(array2, array3, array4);
console.log(resultArray); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
2. Using ES6 spread syntax
In this example, we use the spread syntax (...) to concatenate multiple arrays.
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b'];
const array2 = ['c', 'd'];
const array3 = ['e', 'f'];
const array4 = ['g', 'h'];
const resultArray = [...array1, ...array2, ...array3, ...array4 ];
console.log(resultArray); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']