Array Concatenation

Array concatenation, like string concatenation, is the combining of two arrays together. There are two convenient techniques for doing just that.

The concat() Method

The concat()open in new window method is used to merge two or more arrays. It does not change existing arrays but instead returns a new array.

const primary = ['red', 'blue', 'yellow']
const secondary = ['purple', 'green', 'orange']


console.log(primary.concat(secondary)) 
// ['red', 'blue', 'yellow', 'purple', 'green', 'orange']

This YouTube video was created by Steve Griffith.

The Spread Syntax

The spread syntaxopen in new window is one of the more confusing and yet incredibly useful features recently introduced to JavaScript. It is a shortcut that will write out or spread all the elements of an array. This can be useful for combining arrays together.

const primary = ['red', 'blue', 'yellow']
const secondary = ['purple', 'green', 'orange']


console.log([...primary, ...secondary]) 
// ['red', 'blue', 'yellow', 'purple', 'green', 'orange']