Follow The New Tutorial updates on WhatsApp, Telegram or YouTube.

JavaScript Array concat() Method

The JavaScript array concat() method combines two or more arrays.

The concat() method returns a new array, containing the joined arrays.

JavaScript Array concat() Method doesn’t make any change in the original array.

Syntax

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

The arr.concat() method is used to merge two or more arrays together. 

array2,array3,….,arrayN – It represent the arrays to be combined.

The number of arguments to this method depends upon the number of arrays or values to be merged.

Return value

A new array object that represents a joined array.

Examples

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]

const letters = ['a', 'b', 'c'];
const numbers = [1, 2, 3];

const alphaNumeric = letters.concat(numbers);
console.log(alphaNumeric);
// results in ['a', 'b', 'c', 1, 2, 3]

Concatenating values to an array

//In this example, we will concat the elements directly.
var arr=["C","C++","Python"];  
var result= arr.concat("Java","JavaScript","Android");  
document.writeln(result);  //C,C++,Python,Java,JavaScript,Android
const letters = ['a', 'b', 'c'];

const alphaNumeric = letters.concat(1, [2, 3]);

console.log(alphaNumeric);
// results in ['a', 'b', 'c', 1, 2, 3]

Concatenating three arrays

const num1 = [1, 2, 3];
const num2 = [4, 5, 6];
const num3 = [7, 8, 9];

const numbers = num1.concat(num2, num3);

console.log(numbers);
// results in [1, 2, 3, 4, 5, 6, 7, 8, 9]

Concatenating nested arrays

const num1 = [[1]];
const num2 = [2, [3]];

const numbers = num1.concat(num2);

console.log(numbers);
// results in [[1], 2, [3]]

// modify the first element of num1
num1[0].push(4);

console.log(numbers);
// results in [[1, 4], 2, [3]]