2

I'm aware of the fact that JavaScript doesn't have finer grained scopes than "global" or "per-function". But is there any real problem with writing something like:

function test() {
    for (var i = 10; i < 20; i++) { /* ... */ }
    for (var i = 03; i < 04; i++) { /* ... */ }
}

It's annoying to have to keep coming up with new names for indexing variables (which is what I've been doing: fooIndex, barIndex, etc.) And moving the declarations to the top of the function isn't great either:

function test() {
    var i;
    for (i = 10; i < 20; i++) { /* ... */ }
    for (i = 03; i < 04; i++) { /* ... */ }
}

Then if your uses are far away from the declaration, you might forget to delete unused variables. Or you might accidentally write into global scope because you thought you had var-declared something you didn't.

It seems a lesser evil to put the var on anything you intend to be local. Is it? According to this answer redefining it as var more than once has no effect, but that situation is more contrived than loop variables. Is there a truly good reason not to do things like the first case above?

Community
  • 1
  • 1
  • 2
    It is *only* a stylistic issue - I personally prefer to keep the "var close" (and use a different variable name), but different linters and static analysis tools have their own prejudices. Although, I would not prefix numbers with 0 unless I *wanted* octal ;-) – user2864740 Feb 02 '14 at 03:36
  • (`var` is a scope-wide declaration, not an "executed statement"; due to "hoisting" the placement of `var`, within the same function/program scope, has *no effect* on the correctness of a program.) – user2864740 Feb 02 '14 at 03:39
  • @useruser2864740 My hard part is to keep coming up with a bunch of names for "index". I might stick with my current strategy (yours), or give myself up to the linter to catch the issues that arise from putting the declarations at the top. I don't know. Sigh, JavaScript, you wound me... *(Note: As it happens in this case, I do want octal. :-) [My alter-ego Dr. Rebmu can be a trickster](http://codegolf.stackexchange.com/a/16712/57))* – HostileFork says dont trust SE Feb 02 '14 at 03:40
  • I don't loop by index much .. where I do it is very rarely within the same function or the same source. I like to write smaller functions and use functional-style iterators when possible. Besides, there is still `j` and `k` ;-) – user2864740 Feb 02 '14 at 03:41
  • Agreed no. I think the first way reads better because the change makes no difference in operation but is stylistically different. Looking at the code I assume there is a change in functionality and needlessly close read the function. Convention makes expression. – Dan Baker Feb 02 '14 at 03:42
  • 1
    There is no real risk of hitting an undeclared `var` in `strict mode`, but I think it's just a matter of taste, I'm used to declare everything on top, and set `strict mode` – axelduch Feb 02 '14 at 03:44
  • @aduch That looks like an answer to me, if you want to make it one. Strict mode seems to solve at least half of the problem (though it doesn't do anything about unused variables, but I guess the occasional linting can catch that). – HostileFork says dont trust SE Feb 02 '14 at 03:48

1 Answers1

3

As I stated in the comment above, There is no real risk of hitting an undeclared var in strict mode, but I think it's just a matter of taste, I'm used to declare everything on top, and set "strict mode".

However, I agree that it doesn't solve the problem of resetting variables. I usually rather have a consequent number of verbose variable names, but the choice is yours and only do what suits you better as long as it doesn't harm your debug processes.

Also, I would like to insist on the fact, that "strict mode" should always be on, it'll help you a lot for debugging.

EDIT: if you are not familiar with "strict mode", you can have a detailed explanation here on mdn

axelduch
  • 10,769
  • 2
  • 31
  • 50
  • 1
    Strict mode was new to me, thanks. I tinker with JavaScript now and again because...well, I have to in order to run code in a browser. Personally looking forward to when [Red](http://www.red-lang.org/p/contributions.html) gets a JS backend and I never have to program directly in JavaScript again (!) :-/ But until then, thanks for the info, I'll be putting this on all my files now. – HostileFork says dont trust SE Feb 03 '14 at 10:54