/ Published in: JavaScript
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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]);