|
|
|
|
|
by jacquesm
5623 days ago
|
|
That's a good rule. I usually declare variables as close to their intended scope of use as I can get them, so for instance, right inside the block where the variable is going to be used. The temptation then becomes to re-use the name but I avoid that to make it easier to see which declaration belongs to which variable. Declaring all the variables at the top of a function was a pretty hard to break habit. |
|
Which is much more preferable than scattered around the function. Especially due to hoisting[1].
My functions typically go in the order of:
declare variables; declare any local functions if necessary (most of the time these eventually get refactored out to somewhere else, because they tend to be one-off utility functions that can be abstracted); do stuff; return value.
1. http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-...