Comments on: Dirty looping in JavaScript geek, punk, pussy … Sat, 05 Oct 2013 14:25:34 +0000 hourly 1 By: Jose Fri, 30 Nov 2012 03:26:29 +0000 Good one! Hoisting is a new concept to me, you helped clarify it. This page is good too:

By: Ivan Krechetov Tue, 17 Aug 2010 13:54:18 +0000 Nice tip, thanks! I also enjoyed the previous posts.

I guess, another fun thing to do is passing parameters in this nested loop’s function:


Thus, the enclosing loop’s i becomes a j in the nested loop.

By: Pierre Spring Thu, 27 May 2010 12:17:35 +0000 @BOobby: the problem with let is that the scope of a variable declared inside a let statement using var is still the same as if it had been declared outside the let statement – such variables still have function scoping.

In the above example, var row; would still be in the scope of the whole function.

And of course, let only runs in FF >= 3.0 …

By: BObby Thu, 27 May 2010 10:28:25 +0000 Or you could use the let keyword in JavaScript 1.7+, but of course there are problems with this too.

By: Pierre Spring Thu, 27 May 2010 06:42:39 +0000 Freeman: The point I am trying to make is not to get the enclosing scope of a loop dirty. i and row declaration both get hoisted to the top if you don’t wrap the loop in a function. And this can have unexpected results.

The assure_positive() is for educational purpose and therefor it is kept very simple.

By: A Freeman Thu, 27 May 2010 00:21:41 +0000 Why would you reuse the outer loop index for the inner loop, in the first place? Am I missing something here?

for (i=0;i<matrix.length;i++) {
    var row=matrix[i];
    for (j=0;j<row.length;j++) {
        // stuff happens here
By: Pierre Spring Tue, 25 May 2010 08:07:09 +0000 @jordi: Where you put the closing parenthesis does not matter really, it is a question of convention. Crockford demands that you close it the way it’s done in my examples, arguing that the way you do it makes no sense (c.f. video 3 at 29’28”)

To me both make perfect sense, yet I choose to adhere to the standards proposed by Crockford.

The only important thing is to have the opening parenthesis, in order to not to have a function expression rather than a function statement. The statement would not be callable.

By: Jordi Boggiano Mon, 24 May 2010 14:56:20 +0000 Interesting method. Just a note on using one-shot functions as you did, I usually saw the function wrapped in parenthesis’ and then executed, rather than the execute then wrap that you have there, so that would look like:


I’m not sure if there is a difference between the two as far as browser support goes or anything, but if the other induces errors, you may want to try that.