This post will discuss how to copy elements of an array into another array in JavaScript.

The solution should add elements of an array to another array and should not create a new array.

1. Using Array.prototype.push() function

To append values of an array into another array, you can call the push() method of the Array object.

Download  Run Code

2. Using Function.prototype.apply() function

Alternatively, the apply() method can be used with the push() method in the following manner. The apply() method simplify calls the push() method for elements of the specified array.

Download  Run Code

3. Using Spread operator

The code can be simplified using the array spread syntax since the push() method can accept multiple parameters. Using the Spread syntax, you can expand the array expression inside the push() method, which expects zero or more arguments.

Download  Run Code

 
Note that the spread syntax is not supported with IE browser and earlier versions of Edge browser.

That’s all about copying elements of an array into another array in JavaScript.