Languages
[Edit]
EN

JavaScript - concatenate multiple arrays

3 points
Created by:
Jacob
532

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']

 

See also

  1. JavaScript - merge two arrays

References

  1. Spread syntax (...) - JavaScript | MDN
  2. Array.prototype.concat() - JavaScript | MDN

Alternative titles

  1. JavaScript - concatenate n arrays
  2. JavaScript - concat multiple arrays
  3. JavaScript - merge multiple arrays
  4. JavaScript - concat n arrays
  5. JavaScript - merge n arrays
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join