Using the jQuery for-loop is quite slow, which is the reason why I'm considering using the regular for-statement more often. In order to have direct access to the current element, I found the following syntax (for regular arrays, not for objects of course):
for (var i = 0, e; e = array[i]; i++) { ... }
where e
in the loop represents the current element.
Is this syntax safe to use across all browsers?
Addition
OK, I guess this could work, but it is not so useful anymore for a short notation:
for (var i = 0, e; (e = array[i]) !== void(0); i++) { ... }
Thank you all for answering!
I do not recommend that. You see, if your array looks like this for example:
array = ["lala", 078, false, 992, "kas"];
Then your loop would only go through the first two, since the term e = array[i];
would return false, because the third entry in the array is literally false.
This is better:
for (var i = 0, e; (e = array[i])===undefined; i++) { ... }
Make sure no one overwrites the undefined variable, e.g. by using a closure: How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?