Wait. Why is that script tag in the body?

  • Browsers read HTML in order
  • If scripts show up before elements, they can't read/update the elements
  • The order of multiple <script> tags matters a lot too
<html>
    <head>
        <script>
            const a = document.getElementById("heading");
            // a is null
        </script>
    </head>
    <body>
        <h1 id="heading">My Page</h1>
    </body>
</html>

22 / 24