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:
xxxxxxxxxx
1
const array1 = ['A', 'B'];
2
const array2 = ['C', 'D'];
3
4
const array3 = array1.concat(array2);
5
6
console.log(array3);
In this section, we present how to merge two arrays using the spread syntax.
xxxxxxxxxx
1
const array1 = ['A', 'B'];
2
const array2 = ['C', 'D'];
3
4
const array3 = [array1, array2];
5
6
console.log(array3);
Output:
xxxxxxxxxx
1
[ 'A', 'B', 'C', 'D' ]
xxxxxxxxxx
1
const array1 = ['A', 'B'];
2
const array2 = ['C', 'D'];
3
4
array1.push(array2);
5
6
console.log(array1);
Output:
xxxxxxxxxx
1
[ 'A', 'B', 'C', 'D' ]