Your problem in the first case is the area. By placing var init inside a function declaration, you checked it for access inside that function.
Something else happens in the second case; it will take more code to see that.
Global scope in Javascript window . This means that when you declare variables directly in the <script> tags, you can return them by requesting window.variableName .
A common way to solve such problems is to create a namespace structure. If you do it right, you can call myNamespace.subNamespace.variable and have full confidence that since it is explicitly bound to a window , you can return it no matter where you are.
Remember that jQuery is built into Javascript. This is nothing special.
source share