I have asked a similar question before but I never made my point exactly clear, or at least I think it’s such a relevant question that it’s worth to bring it up and see if anyon
One advantage would be, that you are able to place the code anywhere in the page. In our case, we use a templating system in our CMS that stitches the page together from around 10 - 30 templates for the different parts (depending on complexity).
Since you want the templates to work on any page they are used, you need to include the necessary Javascript in it. For those cases, the ready()
function is a real life saver.