Our general coding practices recommend declaring a local variable as close as possible to their first use:

https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style

I think that's a sensible directive and there's no reason not to employ it in JavaScript too.