How to Merge Two Array in JavaScript | Step by Step Guide

How to Merge Two Array in JavaScript

Learn how to merge two array in javascript with these simple steps.

1. Creating an array

To create an array in javascript, we need to use the “const” or “var” keyword then we give array name and assigned the array values within the square brackets [ ]. So, here we have created two arrays.

Ex:

const Fruits = ["Apple", "Orange", "banana"];
const Vegitables = ["Onion", "Tomato", "Beans", "Potato"];

2. Merging two arrays

To merge the two lists of array values, we need to use “concat()” method in . Concat() returns a new array by concatenating/appending given element(s) to the elements of the given array.

Syntax:

concat(..args): Arrays

Where:
…args = indicates elements to be concatenated/appended

Ex:

const shop = Fruits.concat(Vegitables);
document.write(shop);

Here, I merged the Fruits and Vegetables array using concat() method and store it as “shop” variable.
Then printed the “shop”. See the output, the two arrays is merged and displayed.

Output:

merge two array in javascript

Leave a Reply