Question: Can we use any type of javascript code as a external .js file or sometime it's necessary to place in `<head>`?

Answer: 
The only time you would ever need to inline a js function in your HTML using the <SCRIPT> tags is if your javascript is generated by your server side program depending on the data, user settings etc.

Even this case is extemely rare as as you should be able to create a .js function whose behaviour is controlled by passing parameters.

Apart from keeping everything tidy and in the place where you expect to find it, there is a network performance advantage in that \*.js files are cached on the client side so you are not constantly sending the same stuff over the network again and again.
if using jquery then `$.document.ready()` is the way to go