How to combine two array in JavaScript?

Taimoor Sattar

You can combine arrays in Javascript in two ways:

  • concat (ES5 version)
  • Spread operation / destructuring (ES6 version)

So, let suppose you have arrays in Javascript as below.

var fruit1 = ["apple", "banana"];
var fruit2 = ["oranges", "grapefruits", "mangoes"];
var fruit3 = ["strawberries", "raspberries", "blueberries"];

use concat (ES5 version):

If you want to support the old browser, you can use the ES5 version concat in Javascript. Perform the combination operation javascript as below.

var cart1 = fruit1.concat(fruit2);
// ["apple", "banana", "oranges", "grapefruits", "mangoes"]

var cart2 = fruit3.concat(fruit1, fruit2);
// ["strawberries", "raspberries", "blueberries", "apple", "banana", "oranges", "grapefruits", "mangoes"]

Further, you can also write the command as below:

var fruits2 = [].concat(fruit1, fruit2, fruit3);

Spread operation / destructuring (ES6 version)

Spread operation is easy to write in Javascript . Perform the combination operation using spread operation in javascript as below:

const cart3 = [...fruit1, ...fruit2, ...fruit2];

// ["strawberries", "raspberries", "blueberries", "apple", "banana", "oranges", "grapefruits", "mangoes"]

About the Author

Written by Taimoor Sattar I'm Taimoor Sattar, a full-stack developer, experience to develop websites in React/JavaScript, Node, and HTML/CSS. You should follow them on Twitter. You can contact me on this page.