Definition and Usage
concat () method is used to connect two or more arrays.
This method does not change the existing array, but only returns a copy of the array is connected.
grammar
arrayObject.concat(arrayX,arrayX,......,arrayX)
parameter | description |
---|---|
arrayX | essential. The parameter may be a specific value, but may be an array of objects. It can be any number. |
return value
Returns a new array. The array is obtained by adding all the parameters arrayX arrayObject generated. If you want to concat () is an array of operating parameters, adding that elements in the array, instead of an array.
Examples
Example 1
In this example, we will concat () is a parameter connected to the array:
<script type="text/javascript"> var a = [1,2,3]; document.write(a.concat(4,5)); </script>
Output:
1,2,3,4,5
Example 2
In this example, we create two arrays, and then use the concat () to connect them:
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" document.write(arr.concat(arr2)) </script>
Output:
George, John, Thomas, James, Andrew, Martin
Example 3
In this example, we created three arrays, and then use the concat () to connect them:
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" var arr3 = new Array(2) arr3[0] = "William" arr3[1] = "Franklin" document.write(arr.concat(arr2,arr3)) </script>
Output:
George,John,Thomas,James,Adrew,Martin,William,Franklin