Concatenate Two or More Arrays
In JavaScript, Array.concat()
method is used to concatenate two or more arrays.
Syntax
array1.concat(array2)
array1.concat(array2, array3)
array1.concat(array2, array3,.., arrayN)
array1
,array2
,array3
,array4
, ..,arrayN
are arrays
concat()
method takes one or more arrays as arguments, concatenates them with the calling array, and returns the resulting array. Original array, or arrays passed as arguments remain unmodified.
Examples
1. In the following program, we take two arrays and concatenate them.
const array1 = ['apple', 'banana', 'cherry'];
const array2 = ['mango', 'guava'];
const output = array1.concat(array2);
console.log(output);
2. In the following program, we take three arrays and concatenate them.
const array1 = ['apple', 'banana', 'cherry'];
const array2 = ['mango', 'guava'];
const array3 = ['orange', 'lemon'];
const output = array1.concat(array2, array3);
console.log(output);