Mehedi Hassan Piash [Sr. Software Engineer]

January 17, 2011

Two Ways to Merge Arrays in JavaScript

January 17, 2011 Posted by Piash No comments

Here are two ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat.


// 2 Ways to Merge Arrays
const cars = ['🚗', '🚙'];
const trucks = ['🚚', '🚛'];
// Method 1: Concat 
const combined1 = [].concat(cars, trucks);
// Method 2: Spread
const combined2 = [...cars, ...trucks];
// Result
// [ '🚗', '🚙', '🚚', '🚛' ]