Below is the example of Array concat() method to join three arrays.
- Example:
<script>// JavaScript code for concat() methodfunctionfunc() {varnum1 = [11, 12, 13],num2 = [14, 15, 16],num3 = [17, 18, 19];document.write(num1.concat(num2, num3));}func();</script>chevron_rightfilter_none - Output:
[11,12,13,14,15,16,17,18,19]
The arr.concat() method is used to merge two or more arrays together. This method does not alter the original arrays passed as arguments.
Syntax:
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
Parameters: The parameters to 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 examples illustrate the JavaScript Array concat() method:
- Example 1: In this example, the method concat() concatenates all the three arrays into one array which it return as the answer.
var num1 = [11, 12, 13], num2 = [14, 15, 16], num3 = [17, 18, 19]; print(num1.concat(num2, num3));Output:
[11,12,13,14,15,16,17,18,19]
- 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 return as the answer.
var alpha = ['a', 'b', 'c']; print(alpha.concat(1, [2, 3]));
Output:
[a,b,c,1,2,3]
- Example 3: In this example, the method concat() concatenates both the arrays into one array which it return as the answer.
var num1 = [[23]]; var num2 = [89, [67]]; print(num1.concat(num2));
Output:
[23,89,67]
More example codes for the above method are as follows:
Program 1:
<script> // JavaScript code for concat() method function func() { var alpha = ["a", "b", "c"]; document.write(alpha.concat(1, [2, 3])); } func(); </script> |
Output:
[a,b,c,1,2,3]
Program 2:
<script> // JavaScript code for concat() method function func() { var num1 = [[23]]; var num2 = [89, [67]]; document.write(num1.concat(num2)); } func(); </script> |
Output:
[23,89,67]
Supported Browsers: The browsers supported by JavaScript Array concat() method are listed below:
- Google Chrome
- Internet Explorer
- Mozila Firefox
- Safari
- Opera
Recommended Posts:
- TypeScript | Array concat() Method
- JavaScript | String concat()
- Collect.js concat() Method
- Node.js | Buffer.concat() Method
- TypeScript | String concat() Method
- JavaScript | Array from() Method
- JavaScript Array some() Method
- JavaScript Array from() Method
- JavaScript Array map() Method
- JavaScript Array every() Method
- JavaScript Array pop() Method
- JavaScript | Array map() Method
- JavaScript Array push() Method
- JavaScript Array forEach() Method
- JavaScript Array fill() Method
- JavaScript Array lastIndexOf() Method
- JavaScript Array shift() Method
- JavaScript Array unshift() Method
- JavaScript Array reduceRight() Method
- JavaScript Array keys() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


