How to Create a New Array from Two Arrays in JavaScript?

Estimated read time 1 min read

You can create a new array from two arrays in JavaScript by using the concat() method. The concat() method combines the elements of two or more arrays and returns a new array that contains all the elements.

For example:

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var newArray = array1.concat(array2);

console.log(newArray); // Output: [1, 2, 3, 4, 5, 6]

Alternatively, you can also use the spread operator (...) to merge two arrays into a new array:

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var newArray = [...array1, ...array2];

console.log(newArray); // Output: [1, 2, 3, 4, 5, 6]

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply