The next links explain very well how JavaScript’s scoping system works as the related topic known as hoisting.
- http://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/
- http://www.sitepoint.com/back-to-basics-javascript-hoisting/
- http://codyburleson.com/2014/10/23/meet-iife-the-quintessential-javascript-closure/
I really hope you find this information useful.