Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



JavaScript ForEach Equivalent

One thing with the For Loop in JavaScript is it doesn't seem to be very well documented that you can use it to do an equivalent of a ForEach loop.

Here's a short example of doing the ForEach loop equivalent in JavaScript:

var names = ["Chris","Kate","Steve"];

for ( var i in names )
{
    alert( names[i] );
}

In the above code, the variable "i" is our iterator and by using the "in" keyword the "for" loop actually loops through all elements in the Array for us. Using this you no longer have to worry about the length of the array.

Currently rated 4.4 by 22 people

  • Currently 4.363636/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Thursday, February 28, 2008 6:23 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann