Where to Put JavaScript

The script Tag

In HTML, JavaScript goes inside <script>, either inline or in an external .js file:

<!-- inline -->
<script>
  console.log("Inline script");
</script>

<!-- external file: reusable and cacheable (recommended) -->
<script src="app.js" defer></script>

Head or Body?

PlacementEffect
<script src> in the head, no attributeblocks the page until the script loads: avoid
in the head with deferdownloads in parallel, runs after the HTML is parsed: recommended
at the end of <body>runs after the content above it: the older, still-valid approach

Running JavaScript Outside the Browser

Install Node.js and run a file from the terminal with node app.js. The examples on this site with an Output box run on Node, exactly like the code runner.