Full answer
When the browser parses HTML and meets a plain <script src>, it stops parsing, downloads the file, runs it, and then continues. That blocks the page. async and defer let the download happen in parallel with parsing, but they run the code at different times.
- Normal: download and run immediately at that spot, parsing waits.
async: download in parallel, run as soon as it is ready. Parsing pauses only for the run itself. Order between several async scripts is not guaranteed.defer: download in parallel, run after the document has been fully parsed, just beforeDOMContentLoaded, in the order the tags appear.type="module"scripts behave likedeferby default.
async and defer have no effect on inline scripts without src. The demo uses tiny data: URL scripts so it needs no files.
The log always starts normal, and has inline after it, then defer-1, defer-2 and DOMContentLoaded at the end. Where async appears can change from run to run, because it depends on timing.
A simple rule: use defer for scripts that need the DOM or depend on each other, use async for independent scripts such as analytics, and avoid plain blocking scripts in the head.
<script>window.log = [];</script>
<script src="data:text/javascript,log.push('normal')"></script>
<script src="data:text/javascript,log.push('defer-1')" defer></script>
<script src="data:text/javascript,log.push('defer-2')" defer></script>
<script src="data:text/javascript,log.push('async')" async></script>
<script>log.push('inline');</script>
<pre id="out">waiting</pre>
<script>
document.addEventListener("DOMContentLoaded", () => {
log.push("DOMContentLoaded");
document.getElementById("out").textContent = log.join(" > ");
});
</script>The async script can appear anywhere before DOMContentLoaded, so its position is not fixed; the check ignores it. The scripts are data: URLs so no files are needed. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Normal scripts block the parser at their position.
- async scripts run as soon as they are downloaded, in any order.
- defer scripts run after the document is parsed, in order, before DOMContentLoaded.
- async and defer do nothing for inline scripts without src.
- type=module scripts are deferred by default.
Common mistakes
- putting async on scripts that depend on each other, such as a library and the code that uses it. They can run in the wrong order.
- expecting defer to work on an inline script. Move the code to a file or wrap it in a DOMContentLoaded listener.
Follow-ups
- does defer delay DOMContentLoaded?
- Which of async and defer is safe for scripts that depend on each other?
- What difference does it make to put a script at the end of the body instead of using defer?
Tips
- Add defer to your own application scripts in the head. You get early download and an execution order that is easy to reason about.
- Draw a short timeline with parsing, download and execution bars for each case if you get a whiteboard.
- Give a real example for each: defer for the app bundle, async for analytics.