| |
|
Displaying all elements of an array |
We can display all elements of an array by looping through each element of the array. Here we will increment the index of the array by one in each loop and display elements of the array one by one. To identify how many loops we have to make we must know the total number of elements present inside the array. That we can find out like this
array_name.length
So this number we will set as upper limit of our number of rotation or looping. You can check our tutorial on creating array to create an array and now we will try to display each element of the array. Here is the code.
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}
The for loop in the above code loops through the elements of the array and display one by one vertically by adding one line break at the end of each element. The upper limit of the for loop is set to script.length value which is in this case equal to 4
Displaying elements of an array by using join method
|
|
|
| Array Functions |
|
|
|
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|