TExcelTAD
Home › JavaScript › JS Where To

JavaScript Where To

To run JavaScript in a web page you place it inside a <script> element. There are three common places for it: inline in the <head>, inline at the end of the <body>, or in a separate .js file that the page links to.

The <script> element

Anything between <script> and </script> is treated as JavaScript and executed by the browser:

html · try it yourself
<!doctype html>
<html>
  <body>
    <p id="msg">Waiting for JavaScript…</p>
    <script>
      document.getElementById("msg").textContent = "JavaScript ran!";
      console.log("Script finished");
    </script>
  </body>
</html>

Head or body?

The browser reads an HTML page from top to bottom. When it reaches a classic <script>, it stops, runs the script, and only then continues building the page. That has an important consequence: a script in the <head> runs before the elements in the body exist.

html · try it yourself
<!doctype html>
<html>
  <head>
    <script>
      // The paragraph below does not exist yet, so this finds nothing.
      console.log("In head:", document.getElementById("late"));
    </script>
  </head>
  <body>
    <p id="late">I am a paragraph.</p>
    <script>
      console.log("End of body:", document.getElementById("late").textContent);
    </script>
  </body>
</html>

The first script prints null because the paragraph hasn't been parsed yet. That's why scripts were traditionally placed just before </body>.

External scripts

For anything bigger than a few lines, put the code in its own file and reference it with the src attribute:

html
<!-- index.html -->
<script src="app.js"></script>
js
// app.js — no <script> tags inside a .js file
console.log("Hello from an external file");

Advantages of external files:

Benefit Why it matters
Separation HTML stays about structure; logic lives in .js files
Caching The browser downloads app.js once and reuses it on every page
Reuse Several pages can share the same script
Tooling Editors, linters and bundlers (like Vite) work best with real .js files

The src can be a relative path (js/app.js), an absolute path (/scripts/app.js) or a full URL to a CDN.

defer and type="module"

Modern pages usually keep their scripts in the <head> but tell the browser not to block:

html
<head>
  <!-- download in parallel, run after the HTML is parsed, in order -->
  <script src="app.js" defer></script>

  <!-- ES modules are deferred automatically and may use import/export -->
  <script type="module" src="main.js"></script>
</head>
Attribute Blocks parsing? Runs when Order kept?
(none) Yes Immediately Yes
defer No After the HTML is parsed Yes
async No As soon as it's downloaded No
type="module" No After parsing (like defer) Yes

A deferred or module script can safely use any element on the page, wherever the tag is:

html · try it yourself
<!doctype html>
<html>
  <head>
    <script type="module">
      document.getElementById("out").textContent = "Module scripts wait for the page.";
    </script>
  </head>
  <body>
    <p id="out">(not set yet)</p>
  </body>
</html>

Tip: The JavaScript playgrounds in this course run your code as an ES module, exactly like <script type="module">. That's why examples can use import, top-level await, and are always in strict mode.

Inline event attributes

You may also see JavaScript inside HTML attributes like onclick="…". It works and is handy for tiny demos, but in real projects prefer addEventListener (covered in the DOM chapters), which keeps behaviour out of your markup.

Test yourself

Why might document.getElementById return null in a classic script inside <head>?

  1. Scripts in the head cannot use document
  2. The element has not been parsed yet
  3. IDs only work in external files
  4. Head scripts run last

Which attribute links an external JavaScript file?

  1. href
  2. link
  3. src
  4. file

Which script type is deferred automatically and may use import?

  1. type="text/javascript"
  2. async
  3. type="module"
  4. nomodule