Languages
[Edit]
EN

JavaScript - merge two arrays

0 points
Created by:
elmer
646

In this short article, we would like to show how to merge two arrays in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const array1 = ['A', 'B'];
const array2 = ['C', 'D'];

const array3 = array1.concat(array2);

console.log(array3);

 

More examples

In this section, we present how to merge two arrays using the spread syntax.

Example 1

// ONLINE-RUNNER:browser;

const array1 = ['A', 'B'];
const array2 = ['C', 'D'];

const array3 = [...array1, ...array2];

console.log(array3);

Output:

[ 'A', 'B', 'C', 'D' ]

Example 2

// ONLINE-RUNNER:browser;

const array1 = ['A', 'B'];
const array2 = ['C', 'D'];

array1.push(...array2);

console.log(array1);

Output:

[ 'A', 'B', 'C', 'D' ]

See also

  1. JavaScript - merge two sorted arrays in optimal way

References

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

Alternative titles

  1. JavaScript - join two arrays to concatenate into one array
  2. JavaScript - combine two arrays to concatenate into one array
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