Nice article on: JavaScript variable scope hoisting
https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting Things to Remember All declarations, both functions and variables, are hoisted to the top of the containing scope, before any part of your code is executed. Functions are hoisted first, and then variables. Function