Merge two Arrays In JavaScript


Merge two Arrays In JavaScript – There are many ways to merge two arrays in JavaScript. You can use built-in method to merge the two arrays in JavaScript. Here in this tutorial we are going to explain how to use the concat method to merge the two arrays in JavaScript. You can try the example with our online editor.


How to Merge two Arrays In JavaScript

You can use array.concat to merge two arrays in JavaScript. Here is an example of merging two arrays in javaScript.

Merge two Arrays In JavaScript Example:

<script type='text/javascript'?
var array3 = [];
   var array1 = [2,7,3];
   var array2 = [5,4,1];
   var array3 = array1.concat(array2);
   alert(array3);
</script>

Try it »

Here in the above example we have create three arrays array1, array2 and array3. array3 contains the merged elements of both array - array1, array2. The contact method will merge the array1 and the array2. The resulted array is stored in the array3.

If you run the above example it will produce the output something like this -

Merge two Arrays In JavaScript


Advertisements

Add Comment

📖 Read More