.concat()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 5, 2021Updated Oct 6, 2021
Contribute to Docs

The .concat() array method merges, or concatenates, two or more arrays.

Syntax

The .concat() method does not change the existing arrays, but instead returns a new array.

array1.concat(array2);

It can also concatenate more than two arrays:

array1.concat(array2, array3, array4);

Example 1

Concatenating two arrays:

const grocery1 = ['blueberries', 'eggs', 'artichoke'];
const grocery2 = ['milk', 'cookies'];
const grocery = grocery1.concat(grocery2);
console.log(grocery);
// Output: [ 'blueberries', 'eggs', 'artichoke', 'milk', 'cookies' ]

Example 2

const balls1 = ['⚽', '🏈', '⚾', '🎾'];
const balls2 = ['🏀', '🏐'];
const balls = balls1.concat(balls2);
console.log(balls);
// Output: [ '⚽', '🏈', '⚾', '🎾', '🏀', '🏐' ]

Codebyte Example

Concatenating three arrays:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy