|
| HOW TO LOOP THROUGH AN ARRAY IN JAVASCRIPT
If you have an array created in javascript and you want to perform certain action/s on some or all of the elements, you will have to loop through them. This is achieved by using a for loop as the following example illustrates:
var scores=new Array("204", "155", "278");
var sizeScores=scores.length;
for(i=0; i < sizeScores; i++)
{
document.write(scores[i] + "<br>");
}
The above example will display all the array elements as follows:
Now suppose we want to be selective and display only the score that is more than 200, then we do:
var scores=new Array("204", "155", "278");
var sizeScores=scores.length;
for(i=0; i < sizeScores; i++)
{
if(scores[i] > 200)
{
document.write(scores[i] + "<br>");
}
}
The above example will display only the array elements that have a value exceeding 200 as follows:
Now Read:
|
|