I’m sure you must have had a scenario where you had to join or merge two different Arrays. Well, let me tell you that its an easy task. JavaScript has a native method Array.concat() which does it. Let’s take a look at the syntax.

Please note that the Array here is not the Array object but an Array variable previously declared.

The Array.concat() method accepts two or more array to be concatenated or merged and returns the merged array. Let’s take a look at an example:

var arr1 = ['red','green'];
var arr2 = ['blue','yellow'];
var arr3 = ['brown','black'];

//Now let's merge arr1 with the others
var newArr = arr1.concat(arr2,arr3);

//gives ["red", "green", "blue", "yellow", "brown", "black"]

//gives ["red", "green"]

If you notice we concatenated the 1st array variable (arr1) with the other ones. Yes, that’s how the method works. You need to use the .concat() method on one of the existing Arrays to merge it with other arrays.  The original arrays would not get altered. Instead a new array is created and assigned to the variable you declared.

Hope you found the article to be useful. Do let us know your comments. Also do share it with your friends if you found this article to be useful.