JavaScript ForEach Equivalent

28. February 2008

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.

General ,



Comments

12/29/2008 6:32:19 PM #
Pingback from dmry.net

JavaScript ile foreach | Günlük Haftalık Aylık
6/13/2009 12:56:51 AM #
Pingback from techandwye.wordpress.com

foreach equaivalent in javascript « Tech & wye
8/20/2009 4:18:25 PM #
Pingback from downloadsen.com

JavaScript ile foreach | DownloadSEN
11/23/2009 7:38:13 PM #
Pingback from blog.worldofict.nl

Javascript foreach | Code snipplets
11/29/2009 7:54:48 AM #
Pingback from viciao2k3.net

php ftp light - ftp ligero opensource — Viciao2k3
Comments are closed