JavaScript Combine Two Arrays
Array.prototype.push()
The push() method adds one or more elements
to the end of an array and returns the new length of the array.
Syntax
push(element0)
push(element0, element1)
push(element0, element1, ... , elementN)
Example
const array1 = [1, 2, 3, 4];
const array2 = [5, 6, 7, 8];
array1.push(...array2);
// expected output: Array [1, 2, 3, 4, 6, 7, 8]
Array.prototype.concat()
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array
.
Syntax
concat()
concat(value0)
concat(value0, value1)
concat(value0, value1, ... , valueN)
Example
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]