Merge Arrays JavaScript | Concat() Method

Merge Arrays JavaScript | Concat() Method

In this tutorial, you will learn how to merge or combine two or more arrays in javascript with examples.

JavaScript Array concat() Method

Array concat() method is an inbuilt javascript method, which is commonly used to combine or concatenate or merge two or more arrays in javaScript.

Note: This method merges or adds two or more arrays and returns a new single array.

Syntax

The basic syntax of javascript concat() method is following:

array.concat(array_second, array_third, …, array_n);

Parameters of the concat() method

ParameterDescription
array_one, array_two,….. array_nThis is required. The arrays to be merged

Example first – Merge two arrays in javascript

Let’s take the first example, we have two array in javascript. We will merge those arrays using the concat() function of javascript. Let’s see the below:

      var array = ["php", "java"];
      var array2 = ["c", "c#", ".net"];
      var res = array.concat(array2);

      console.log(res);

The output of the above code is: (5) [“php”, “java”, “c”, “c#”, “.net”]

Example Second – Merge 3 arrays in javascript

We have 3 numeric array in javascript. In this example, we will merge or combine three javascript numeric arrays. Let’s see below:

      var array = [1,2,3];
      var array2 = [4,5,6];
      var array3 = [7,8,9,10];
      var res = array.concat(array2, array3);

      console.log(res);

The output of the above code is: (10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Conclusion

In this tutorial, you have learned how to merge two or more arrays together in javascript using the array concat() method of javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *