The push() method is generally used to push an array to the end of an existing one. In ES5 specification, this is often done as follows:
let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; // Append all items from arr2 onto arr1 Array.prototype.push.apply(arr1, arr2) console.log(arr1);
If your browser supports ES6, you can use the spread operator:
let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push(...arr2); console.log(arr1);
The push() method can take more than one parameters, so you can use the apply() method for passing the array of values to be pushed as a list of function parameters:
let arr1 = [0, 1, 2]; let arr2 = [3, 4, 5]; arr1.push.apply(arr1, arr2) console.log(arr1);
let num1 = [1, 3, 8]; let num2 = [2, 5]; num1 = num1.concat(num2); console.log("new numbers are : " + num1);
Beware of using the concat() method which returns a new array instead of changing the existing array containing the values of the merged arrays. However, it is also technically right for this particular situation.