Take the 2-minute tour ×

Trying to think in Javascript rather than jQuery, so I'm wondering if I'm doing this right.

I want to have a callback when my loop is finished. Is this the proper way?


  /* do some stuff */ 

  if ( i === (divs.length - 1)) {  /* call back */  }


I should add that I don't mean something like a JSON request callback, just when the loop has finished.

share|improve this question
Excuse my ignorance, but I don't understand it. If you want to call it when the loop is finished, why not calling after the loop instead of inside it? –  Aleadam Apr 9 '11 at 2:08
If you're going to think in JavaScript, then do it. Do your loop, then call your function. –  JAAulde Apr 9 '11 at 2:11

4 Answers 4

up vote 8 down vote accepted

Why not say what you really mean and call the callback after the loop?

function thing_with_callback(divs, callback) {
        /* do some stuff */ 
share|improve this answer
because that would just be rediculous. plus what if you wanted to edit i interactively in a debugger? –  Thomas Eding Apr 9 '11 at 2:55
Technically, that's not equivalent if the array is empty since it would still run. It's easy to work around though by adding an if statement. –  Matthew Crumley Apr 9 '11 at 3:33
@Matthew: The question was "I want to have a callback when my loop is finished." so putting the callback(); outside the loop is the only way and there is no need for a conditional on divs.length. –  mu is too short Apr 9 '11 at 3:40
Yes, that's probably what he meant to do, but the example in the question wouldn't call the callback if divs.length is zero, because the for loop would never run. You're correct that putting the callback after the loop is almost certainly what he wanted though. –  Matthew Crumley Apr 9 '11 at 5:39
@Matthew: Yeah, there's some ambiguity involved so it could go either way. Hard to say what should happen when divs.length == 0 too. –  mu is too short Apr 9 '11 at 5:49

For clarity, you should go with @mu's answer, but if you really must include the callback within the for construct, you can use the comma operator*:

for(var i = 0;
    i < divs.length || function(){ /* call back */ }(), false;
    i++) {

/* do some stuff */ 


*As explained in this fascinating article.

share|improve this answer
+1 for that article! –  Will Feb 23 '13 at 2:39

Just to make codelahoma's answer simpler you can directly return false from your callback function. (to avoid re-executing the loop's code once more)

for(var i = 0;i < divs.length || function(){ /* callback */ return false;}();i++){
/* loop code */
share|improve this answer
My example doesn't do an extra loop evaluation. The , operator evaluates both operands and returns the result of the one on the right. Did now notice that I was using an expression on the RHS that will always be false, and have edited to reflect that. –  codelahoma Feb 14 '12 at 6:57

It seems to me the question is about the execution order of javascript code. And the answers are:

Yes you can put the callback outside because javascript code is executed line by line. In case of asynchonous ajax calls there might be other things to consider.

share|improve this answer

Your Answer


