0

Given the snippet code from Javascript the good parts (page 24):

var name;
for (name in another_stooge) {
    if (typeof another_stooge[name] !== 'function') {
        document.writeln(name + ": " + another_stooge[name]);
    }
}

Why there is definition of variable name before use in for in loop, since it will work without it?

Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93

5 Answers5

8

There are two different things to pay attention to here.

var

Without var, the variable would be a global unless it was already declared in a wider scope. (In strict mode, it would be an error rather than a global).

Before the loop

You could have for (var name… but that makes it harder to spot the var statement.

Douglas Crockford (the author of The Good Parts, so highly relevant here) advocates declaring all local variables at the top of the function so you have one place to look to find your scope.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Good answer, I would add look into variable hoisting and scoping, as it'll help the issue be much clearer. Check out: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html – trentmwillis May 08 '14 at 21:34
1
var name = 1;
name = 1;

These two are different things. For the first line, name is a variable deaclared under the current function scope, while the second is equivalent to window.name = 1 (if name hasn't been decalred in current scope.) You should never declare temporary variables in the global scope unless you have a really good reason behind it.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • I don't think OP was talking about declaring them in the global scope, but rather inside the `for` statement (i.e. `for (var name...`). – vgru May 08 '14 at 21:44
  • @Groo - He asked in his question why is there a declaration of `name` before the `for` loop as it will work without it. I responded if `name` wasn't declared in the current scope it would be global. – Derek 朕會功夫 May 09 '14 at 00:22
  • Oops, sorry you're right, should've read it more carefully. – vgru May 09 '14 at 08:37
1

It is recommended, that all variables of a function are defined in same place, so if you had multiple variables, that one in the loop would be defined among them:

var a, b, name; //etc
for (name in another_stooge) {
    if (typeof another_stooge[name] !== 'function') {
        document.writeln(name + ": " + another_stooge[name]);
    }
} 

This goes with the section of: "best practices".

Tohveli
  • 487
  • 5
  • 18
1

Yes but in that case name will be a global variable and will behave differently. So if you are after the good parts use var for every variable you use (global variables are not good). But you can shorten the code like this...

for (var name in another_stooge) {

That will make name a scope variable...

0

EDIT: Assuming you are asking why not

for (var name in another_stooge)

A matter of style.

Unlike many other languages, JavaScript is function scoped, not blocked scoped.

As a result, many programmers will manually "lift" their var declarations to the top of the function to make it readily apparent what is going on.

Or -- considering the use of document.write in that code, it could be that the original coder didn't realize there was another way.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74