Merge Arrays JavaScript | Concat() Method

Merge Arrays JavaScript | Concat() Method

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

Merge Arrays JavaScript | 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.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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