Script element
The <script> element is how JavaScript gets into a web page. It either contains code directly or points to an external file with the src attribute. Where you place it and which attributes you add, especially defer, async and type="module", control when the code downloads and runs, which affects both page speed and whether your code can find the elements it needs.
Inline and external scripts
<!-- Inline: code written inside the element -->
<script>
console.log('Hello from an inline script');
</script>
<!-- External: code loaded from a file -->
<script src="js/app.js" defer></script>
External files are usually better. The browser can cache them, several pages can share one file, and HTML and JavaScript stay separate. A script tag with a src must stay empty; code written between its tags is ignored.
How a plain script blocks the page
When the browser reads HTML from top to bottom and meets a normal <script src>, it stops building the page, downloads the file, runs it, and only then continues. A large script in the <head> therefore delays everything visible. Worse, the code runs before the body exists, so document.querySelector('#menu') returns null.
The traditional fix was to put scripts just before </body>. That works, but the download only starts once the browser reaches the bottom.
defer, async and module scripts
| Attribute | Downloads | Runs | Order kept? |
|---|---|---|---|
| none | Immediately, blocking parsing | Right away | Yes |
defer | In parallel with parsing | After the HTML is parsed, before DOMContentLoaded | Yes |
async | In parallel with parsing | As soon as it arrives | No |
type="module" | In parallel | Like defer by default | Yes |
For most of your own code, defer in the <head> is the best default: the download starts early, nothing blocks and the DOM is ready when the code runs. async suits independent scripts like analytics that do not depend on the page or on other scripts.
Module scripts
<script type="module" src="js/main.js"></script>
// js/main.js
import { formatPrice } from './utils.js';
document.querySelector('#price').textContent = formatPrice(499);
Modules can use import and export, run in strict mode automatically, keep their variables private instead of creating globals and are deferred by default. Note that the import path needs the file extension in the browser. Module scripts will not load from a page opened with file://; use a local dev server.
Mistakes with script tags
Using async for scripts that depend on each other is a frequent problem. If app.js uses a library loaded with async, whichever finishes downloading first runs first, so the app sometimes works and sometimes throws "is not defined". Use defer on both to keep order.
Another is writing a self-closing tag, <script src="app.js" />. HTML does not treat it as closed, so the rest of the page can be swallowed as script content. Always write a full </script> closing tag.
A third is a typo in the path. The page shows no error on screen, but the browser console and Network tab report a 404 for the file.
Choosing the right setup
- Prefer external files over inline code.
- Use
deferfor page scripts andasynconly for independent ones. - Use
type="module"when you wantimportand private scope. - Always close the tag with
</script>.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.