Merge Two JavaScript Arrays

3rd July 2008 - 1 minute read time

Here is a simple bit of code that you can use to merge one or more arrays. The function you need is called contact().

Take the following two arrays, both of which contain numbers.

  1. var array1 = new Array(1,2,3,4);
  2. var array2 = new Array(5,6,7,8);

To join these two arrays together we use the concat() function like this.

array1 = array1.concat(array2);

The variable array1 now contains the contents of both arrays.

Add new comment

The content of this field is kept private and will not be shown publicly.