For most scripts, the end of the document, just before the </body>
tag, is the preferred placement because the browser will be done parsing the document and its DOM structure. Furthermore, the script download and execution blocks the rendering of the page, so moving the script to the end improves the perceived performance.
If for whatever reason we need the script to run before the actual content of the page, it should be placed in the <head>
element.
Source: Learning Web Design book