Moved declarations to the top of functions.

In JavaScript variable declarations are moved to the top of the
functions anyway when interpreted so I have moved the declarations here.