Hacker News new | ask | show | jobs
by jonprins 5622 days ago
I tend to do the same thing. Except, since JavaScript doesn't have block scope, it all ends up at the top of the function.

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