Return to Snippet

Revision: 38927
at January 10, 2011 16:54 by danielwrobert


Initial Code
var numbers = [1, 2, 3, 4, 5];

for (var i = 0, var ii = numbers.length;  i  <  ii;  i++)
{
  numbers[i] *= 2;
}

alert("The last item in the array is " + numbers[numbers.length - 1]);

Initial URL
http://www.sitepoint.com

Initial Description
I saw this useful little variation of a for loop in a tutorial by Kevin Yank at Sitepoint. By adding an additional variable "ii" in the loop's arguments, you can avoid having your program calculate the length of your array every time it loops through. Saves on processing. Delightful.

Initial Title
JavaScript For Loop

Initial Tags
javascript

Initial Language
JavaScript