Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog.

JavaScript forEach

I’m used to using PHPs foreach function to iterate through an array. I finally found support for this in javascript the other day. Here is an example of its use.

var myArr = new Array();
myArr[0] = "element 1";
myArr[1] = "element 2";
myArr[2] = "element 3";
myArr.forEach(yell);
function yell(element, index, array)
{
	alert(index+' '+element);
}

This will alert each key in the array and its associated element. Obviously you can do a lot more with this.

Leave a Reply