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

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
JavaScript and Ajax

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.

No related posts.

Leave a Reply