JavaScript Array concat() Tutorial

In this section, we will learn how to concatenate two or more array lists using concat() method in JavaScript.

JavaScript Concatenate Arrays: concat() Method

The `concat()` method is used to join two or more arrays together and create a new array as a result.

Note: The `concat()` method does not change the original arrays and it just returns a new one that contains the entire elements of all the involved arrays.

Array concat() Syntax:

array1.concat(array2, array3, ..., arrayX)

Array concat() Method Parameters:

The arguments to this method is a variable number of array objects that we want to concatenate their elements and create a new array as a result.

Array concat() Method Return Value:

The Array concat() method returns a new one that contains the entire elements of all the involved arrays.

Example: combining arrays in JavaScript

const arr = ["John","Doe","Jack","Omid","Ellen"];

const arr2 = ["Red","Blue","Green"];

const arr3 = ["Apple","Google","Microsoft"];

const final = arr.concat(arr2,arr3);

console.log(final);

Output:

["John", "Doe", "Jack", "Omid", "Ellen", "Red", "Blue", "Green", "Apple", "Google", "Microsoft"]

Example: merge two arrays

const arr = [1,2,3,4,5,6,7,8];

const arr2 = ["Red","Blue","Green"];

const final = arr.concat(arr2);

console.log(final);

Output:

[ 1, 2, 3, 4, 5, 6, 7, 8, "Red", "Blue", “Green”]
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies