Skip to content
Related Articles

Related Articles

JavaScript Array concat() Method

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

The JavaScript Array concat() Method is used to merge two or more arrays together. This method does not alter the original arrays passed as arguments.

Syntax:

var newArray = oldArray.concat(value1[, value2[, ...[, valueN]]])

Parameters: The parameters of this method are the arrays or the values that need to be added to the given array. The number of arguments to this method depends upon the number of arrays or values to be merged.

Return value: This method returns a newly created array that is created after merging all the arrays passed to the method as arguments. 

Below is an example of Array concat() method to join three arrays.

Example 1: 

JavaScript




<script>
    // JavaScript code for concat() method
    function func() {
        var num1 = [11, 12, 13],
            num2 = [14, 15, 16],
            num3 = [17, 18, 19];
        console.log(num1.concat(num2, num3));
    }
    func();
</script>


Output:

[11,12,13,14,15,16,17,18,19]

Example 2: In this example, the method concat() concatenates both arrays into one array which it returns as the answer.

var num1 = [[23]];
var num2 = [89, [67]];
print(num1.concat(num2));

Output:

[[23],89,[67]]   

Example 2: In this example, the method concat() concatenates all the arguments passed to the method with the given array into one array which it returns as the answer.

var alpha = ['a', 'b', 'c'];

JavaScript




<script>
    // JavaScript code for concat() method
    function func() {
        var alpha = ["a", "b", "c"];
        console.log(alpha.concat(1, [2, 3]));
    }
    func();
</script>


Output:

[a,b,c,1,2,3]

Example 3: In this example, the method concat() concatenates both arrays into one array which it returns as the answer.

JavaScript




<script>
    // JavaScript code for concat() method
    function func() {
        var num1 = [[23]];
        var num2 = [89, [67]];
        console.log(num1.concat(num2));
    }
    func();
</script>


Output:

[[23],89,[67]] 

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers: The browsers supported by the JavaScript Array concat() method are listed below:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 5.5 and above
  • Opera 4 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!