How to merge two arrays in JavaScript

  • Tech Area
  • April 24, 2023


I am working on a project where I need to merge arrays in JavaScript. I have two JavaScript arrays. How do I merge two arrays in JavaScript.

var array1 = ["Paul","James"];
var array2 = ["Smith", "Ana"];

Solution

To just merge the arrays like this:

var array1 = ["Paul", "James"];
var array2 = ["Smith", "Ana"];

array3 = array1.concat(array2);

console.log(array3);

Output

[
  "Paul",
  "James",
  "Smith",
  "Ana"
]


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube