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?

7
  • 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 ;-) Commented Feb 2, 2014 at 3: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.) Commented Feb 2, 2014 at 3: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) Commented Feb 2, 2014 at 3: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 ;-) Commented Feb 2, 2014 at 3:41
  • 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 Commented Feb 2, 2014 at 3:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.