Javascript Foreach

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


This tutorial explains For loop in Javascript which is much like foreach in PHP:

Javascript For each Syntax:

var loop = ["javascript_element1", "javascript_element2", "javascript_element3"];
 
for ( var i in loop )
{
    alert( loop[i] );
}

In the example above, we iterate through each of the loop variable and alert for each element in the javascript array. The javascript foreach allows us to not keep track of the length of the array, since, now foreach is iterating through each element in the loop variable.





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.