Monday, 7 January, 2019 UTC


Summary

Basics
The Array concat method combines two or more values into a new array.
const newArray = oneArray.concat(secondArray);
https://scotch.io/embed/gist/412c418bb3b3985889c913a16c89ef7e
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.
https://scotch.io/embed/gist/665cf59c7430f5ab77e0708d31ba3d1e