OneBite.Dev - Coding blog in a bite size

merge two arrays together in Javascript

Code snippet on how to merge two arrays together in Javascript

  var arr1 = [1, 2, 3];
  var arr2 = [4, 5, 6];
  
  // Merging two arrays
  var mergedArr = arr1.concat(arr2);
  
  console.log(mergedArr); // [1, 2, 3, 4, 5, 6]

The above code shows how to merge two arrays in Javascript. The first two lines of code create two array variables, arr1 and arr2, containing 3 elements each. The third line combines the two arrays using the concat() function from the array prototype, which returns a new array containing all the elements from both. Finally the merged array is logged to the console using the console.log() method. The code outputs [1,2,3,4,5,6], which is the combination of the two original arrays.

javascript