The way you are coding is correct script must be at the bottom
According to Yahoo's Best Practices for Speeding Up Your Web Site
Put Scripts at the Bottom tag: javascript The problem caused by
scripts is that they block parallel downloads. The HTTP/1.1
specification suggests that browsers download no more than two
components in parallel per hostname. If you serve your images from
multiple hostnames, you can get more than two downloads to occur in
parallel. While a script is downloading, however, the browser won't
start any other downloads, even on different hostnames. In some
situations it's not easy to move scripts to the bottom. If, for
example, the script uses document.write to insert part of the page's
content, it can't be moved lower in the page. There might also be
scoping issues. In many cases, there are ways to workaround these
situations. An alternative suggestion that often comes up is to use
deferred scripts. The DEFER attribute indicates that the script does
not contain document.write, and is a clue to browsers that they can
continue rendering. Unfortunately, Firefox doesn't support the DEFER
attribute. In Internet Explorer, the script may be deferred, but not
as much as desired. If a script can be deferred, it can also be moved
to the bottom of the page. That will make your web pages load faster.
top | discuss this rule
source: Best Practices for Speeding Up Your Web Site
According to Google Apps Script Best Practices
Load JavaScript last. Many web developers recommend loading JavaScript code at the bottom of
the page to increase responsiveness, and this is even more important
with the HTML service. In the NATIVE sandbox mode, all scripts you
load are scanned and sanitized client-side, which may take a couple of
seconds. Moving your tags to the end of your page will let
HTML content render before the JavaScript is processed, allowing you
to present a spinner or other message to the user.
Source: Load JavaScript last
Variable and Function Hoisting in JavaScript
<script>
function function1 () {
document.getElementById("demo").innerHTML="HEY";
}
function1();
</script>