Why put JavaScript in head

Anything in the head must be completed before the body is loaded, so it is generally a bad idea to put javascript in there. If you need something while the body is loading, or want to expedite some ajax, then it would be appropriate to put it in the head.


The reason behind this is as the Head gets loaded before the body. Any dynamic javascript code that gets executed in the body on load will execute correctly.

If you have javascript that is just before the </body> tag then any javascript calls made to functions by your page as it loads will error.

So yes putting javascript before the </body> tag will load faster. But only if your javascript will be executed after page load via clicks for example.