Print Array Elements
To access an element in the array, use the variable which is assigned it to, followed by the element's index number inside square brackets ([]).
We can print array elements through loops.
We have 12 variables. We will display these 12 variables through loop.
Example: 1
<script type="text/javascript">
var month = new Array("January" , "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for(i=0; i<month.length; i++)
{
document.write(month[i] + "<br>");
}
</script>
var month = new Array("January" , "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for(i=0; i<month.length; i++)
{
document.write(month[i] + "<br>");
}
</script>
Output
Example: 2
<script type="text/javascript">
var names=new Array();
names[0]="Robert";
names[1]="palash";
names[2]="mike";
for (i = 0; i < names.length; i++) {
document.write(names[i] + '<br />');
}
</script>
var names=new Array();
names[0]="Robert";
names[1]="palash";
names[2]="mike";
for (i = 0; i < names.length; i++) {
document.write(names[i] + '<br />');
}
</script>
Output
Note: <br /> can be use in single quote or double quotes.