The <script> tag is how JavaScript gets attached to an HTML page — either written directly inline, or loaded from a separate file. This lesson covers the HTML side; the full JavaScript course elsewhere on this site covers the language itself.
Code written directly between the opening and closing tags.
<script>
console.log('Hello from inline JavaScript')
</script>Code kept in its own .js file and loaded with the src attribute — usually the better choice, since the file can be cached by the browser and reused across pages.
<script src="app.js"></script>| Placement | Effect |
|---|---|
| At the end of <body> | The page's HTML renders first, then the script runs — the traditional, safest default |
| In <head>, with the defer attribute | The script downloads in the background while the page parses, then runs after — modern equivalent, without blocking rendering |
| In <head>, with no defer | The script downloads and runs before the rest of the page parses — blocks rendering, usually avoided |
<head>
<script src="app.js" defer></script>
</head>The most common beginner bug
When in doubt, either place the script tag right before </body>, or use defer in the head — both avoid the common beginner mistake of a script trying to find an element on the page that hasn't loaded yet.