Add two array elements
Object: Array
Method or Function: concat(argument)
Syntax: array1.concat(array2,array3,...,arrayn)
concat() method takes two or more array elements and adds them to the end of array.
Useing the concat() method, returns a copy of an original string and the string passed by argument.
The concat() method does not modify the original string, and only a copy of the result is returned.
Example: 1
<script type="text/javascript">
var month1 = new Array("January" , "February", "March", "April", "May", "June");
var month2 = new Array("July", "August", "September", "October", "November",
"December");
var months=month1.concat(month2);
document.write(months);
</script>
January,February,March,April,May,June,July,August,September,
October,November,December
Another way to display array
Example: 2
<script type="text/javascript">
var month1 = new Array("January" , "February", "March", "April", "May", "June");
var month2 = new Array("July", "August", "September", "October", "November",
"December");
var months=month1.concat(month2);
for(i=0; i<months.length; i++)
{
document.write(months[i] + "<br>");
}
</script>
Here "month1" and "month2" are two arrays and "months" is another array that contains now the elements of "month1" and "month2" arrays.
We could use var months=month2.concat(month1);.
In this case, the order of elements will change.
No comments:
Post a Comment