The <script> tag
The <script> tag embeds or links JavaScript. The async/defer attributes control when the script loads and runs relative to HTML parsing.
Syntax
<script src="app.js" defer></script> Attributes
| src | The URL of an external script file. Without it, the code is written directly between the tags. |
| type | "module" for ES modules (import/export); a plain script otherwise. |
| async | Loads in parallel with HTML parsing and runs as soon as it's loaded — execution order relative to other scripts isn't guaranteed. |
| defer | Loads in parallel but only runs after HTML parsing finishes, in document order. |
Example
<script src="app.js" defer></script>
<script type="module" src="main.mjs"></script>
Notes
- Without async/defer, a script blocks page parsing until it loads and runs — which is why script tags are often placed right before </body>.
- type="module" scripts already behave like defer by default.