How to Merge Two Arrays in JavaScript

  • by Tyler (236)
  • Time to complete: 1 minute

Learn how to merge two arrays together in JavaScript.


Some languages allow you to concatenate arrays using the addition operator, but JavaScript Array objects actually have a method called concat that allows you to take an array, combine it with another array, and return a new array.

var arr1 = [0, 1, 2];
var arr2 = [3, 5, 7];
var primes = arr1.concat(arr2);
// > [0, 1, 2, 3, 5, 7]

As you can see this creates a new array that is the combination of arr1 and arr2. Again, this does not affect either of the original arrays.