Premasagar is basically right. I'll elaborate a bit more.
A browser's HTML parser is incremental by nature. Instead of letting the user wait until it downloads the whole HTML page and its additional elements (.js files, images, etc) it will start rendering after it has enough information.
In the case of JavaScript it is divided into two scenarios.
The first, when the JavaScript is simply embedded inside a <script> tag, the browser will download and parse. If it's a function code it will load it into the JavaScript parser engine, and if it's just a code block, it will execute it as soon as it finishes getting the whole <script> block.
The second scenario is when we have an include statement - <script src="..." />. In this case, it will try, as soon as it can, to download and parse the JavaScript file according to the first scenario (loading functions into the JavaScript parser and executing blocks of code outside a function scope).
That is why certain techniques requires you to use some kind of an event so that the JavaScript you wish to execute will be executed when the whole page finishes loading.