In this tutorial, you will learn how to merge or combine two or more arrays in javascript with examples.
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.
The basic syntax of javascript concat() method is following:
array.concat(array_second, array_third, …, array_n);
Parameter | Description |
---|---|
array_one, array_two,…… array_n | This is required. The arrays to be merged |
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”]
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]
In this tutorial, you have learned how to merge two or more arrays together in javascript using the array concat() method of javascript.
☞ JavaScript Programming Tutorial Full Course for Beginners
☞ Learn JavaScript - Become a Zero to Hero
☞ Javascript Project Tutorial: Budget App
☞ E-Commerce JavaScript Tutorial - Shopping Cart from Scratch