Fraser Hamilton

How to join two arrays in JavaScript

June 16, 2020

There are 2 easy ways to combine two arrays into one in JavaScript. You can use the spread operator introduced in ES6 and combine them like so:

const boys = ['brian', 'james', 'rory']
const girls = ['karen', 'emily', 'louisa']

const boysAndGirls = [...boys, ...girls]

console.log(boysAndGirls)

// ['brian', 'james', 'rory', 'karen', 'emily', 'louisa']

It’s worth nothing that the order in which you spread the arrays inside of the new array will reflect the order of the combined array. So because we spread boys both before girls they come first in our result.

If you need to support older browsers you can also use concat to join two arrays:

const boys = ['brian', 'james', 'rory']
const girls = ['karen', 'emily', 'louisa']

const boysAndGirls = [].concat(boys, girls)

console.log(boysAndGirls)

// ['brian', 'james', 'rory', 'karen', 'emily', 'louisa']


Written by Fraser Hamilton a full stack developer based out of Edinburgh, Scotland.

© 2020, Fraser Hamilton