News for hackers. Stuff that matters.
Re:Why Don't Software Developers Use Static Analysis Tools t... by jnbiche on Friday June 26, 2015 @08:03AM (#9783727)
> it's useless to define a var inside a loop

No, `for` loops have nothing to do with JavaScript scope. What you probably ran into was a situation like this:

    for (var i = 0; i < length; i++) {
      do blah;
    }

    for (var i = 0; i < length; i++) {
      do more blah;
    }
In this case, as a stylistic preference, JSLint will suggest you remove the second var, or you can also declare it at the top of the scope and then omit the var declarations in the `for` loops anyway.

And yes, strictly speaking, the latter option is the most performance-oriented, although most JS interpreters optimize for this not anyway such that it makes no difference.

But this JSLint suggestion really has nothing to do with the `for` loop, this situation would have the same effect:

    var i = 0;
    i++;
    use(i);

    var i = 0;
    i++;
    use(i);
`for` loops have no impact on scope, which is mostly functional, with some prototypical complexities thrown in for good measure.
Re:Why Don't Software Developers Use Static Analysis Tools t... by paulasmuth on Friday June 26, 2015 @08:32PM (#9788532)
I think that was parent's point. Coming from other languages, it's kind of unexpected that a var defined within a curly brace block (if/for/etc) is actually scoped on the whole function in which it appears and not just that block. Hence it's "useless" to define it within the block/loop -- might as well hoist it outside, it's visible there anyway