Computer >> Computer tutorials >  >> Programming >> Javascript

How to merge two arrays in JavaScript?


To merge two arrays in JavaScript, use the Array.concat() method. JavaScript array concat() method returns a new array comprised of this array joined with two or more arrays.

Example

You can try to run the following code to merge two arrays:

<html>    
   <head>      
      <title>JavaScript Array concat Method</title>    
   </head>      
   <body>          
      <script>          
         var alpha = ["a", "b", "c"];          
         var numeric = [1, 2, 3];  
         var alphaNumeric = alpha.concat(numeric);          
         document.write("alphaNumeric : " + alphaNumeric );        
      </script>          
   </body>
</html>

Output

alphaNumeric : a,b,c,1,2,3