JavaScript – Concatenate Array

To concatenate an array to another array in JavaScript, call concat() method on first array and pass the second array as argument to the concat() method.

concat() method returns a new array with elements of the second array concatenated to the elements of first array.

concat() method does not modify the original arrays.

Example

In the following example, we have taken two arrays in arr1 and arr2. We concatenate elements of arr2 to arr1 using concat() method.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<body>
    <pre id="output"></pre>
    <script>
        var arr1 = ['banana', 'mango', 'apple'];
        var arr2 = ['cherry', 'grape'];
        var result = arr1.concat(arr2);

        displayOutput =  'Array 1 : ' + arr1 + '\n';
        displayOutput +=  'Array 2 : ' + arr2 + '\n';
        displayOutput +=  'Result : ' + result + '\n';
        document.getElementById("output").innerHTML = displayOutput;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to concatenate an array to another array using concat() method.