If you want to iterate with a for loop, you need to increase the index:
for(var i=0; i<arr.length; ++i) {
and then you should get the actual value using the index:
var value = arr[i];
.each performs both of these functions for you and passes the values ββto the function:
$(...).each(function(i, value) {
It simply saves the template code by incrementing the index and getting the value at that index.
Variables defined in the .each function .each also closed (i.e., inside closure ), so the equivalent code (considering the loop and closing the variable, as well as setting this , as well as violating the return value of false ) can be something like this:
(function() { for(var i=0; i<arr.length; ++i) { var ret = (function(index, value) {
which is a bit more to enter.
In terms of runtime performance, .each (not surprisingly) slower than the raw for loop, because it does much more than the raw for loop.
source share