JavaScript Array concat()

Basics

The Array concat method combines two or more values into a new array.

const newArray = oneArray.concat(secondArray);

Note: concat() creates a new array. It does not change the original array. To change the original array, look at Array push().

Syntax

const newArray = firstArray.concat(secondArray);

// multiple parameters
const newestArray = firstArray.concat(secondArray, thirdArray, fourthArray);

// adding more than arrays
const newNewArray = firstArray.concat('a message', 50, ['stuff', 'stuff']);

Multiple Parameters

This method can take several parameters. Each parameter will be added to the first array.

thingsToAdd (any type of value)

Values to be joined to the parent array. This can be an array of elements or values passed as arguments.

Returns a new array

The method will return an array containing all the elements from the parent array and the value specified as arguments. If no arguments are provided, it will return an array containing the elements in the parent array.

// create an array
const names = ['John', 'Peter', 'James', 'Pammy'];
const moreNames = names.concat(['Rich', 'Matt']);

console.log(moreNames);
// output: Array ['John', 'Peter', 'James', 'Pammy', 'Rich', 'Matt']

Common Usage and Snippets

Joining Values to an Array

Joining values or arrays to a given array. e.g Adding values to an array without mutating the parent array.