EN
JavaScript - merge two arrays
0
points
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' ]