Full answer
The browser parses HTML from top to bottom. A script tag changes how that goes.
Plain <script src="a.js">: parsing stops, the file downloads, runs, and then parsing continues. Scripts in the head delay the first render, and a script in the head cannot see elements that are below it. It runs in document order with other plain scripts.
<script defer src>: the file downloads while parsing continues, and it runs after the document has been parsed, in the order the tags appear, before the DOMContentLoaded event. It is the best default for scripts that need the DOM. Defer applies only to external scripts.
<script async src>: the file downloads in parallel and runs as soon as it is ready, which can be before or after parsing is done, and the order between async scripts is not guaranteed. Use it for independent scripts, such as analytics, that do not depend on other scripts or on the DOM being complete.
<script type="module">: it is deferred by default (inline module scripts too), runs in strict mode, has its own scope, supports import, runs only once even if included twice, and with async added it runs as soon as it and its dependencies are loaded. Use nomodule to provide a fallback for very old browsers.
Events: DOMContentLoaded fires on document when the HTML is fully parsed and deferred and module scripts have run. It does not wait for images, stylesheets in some cases (they can block scripts), or subframes. The load event on window fires later, when all resources, including images, have loaded. document.readyState goes from loading to interactive to complete.
Practical code: if your script may run before or after DOMContentLoaded (for example, loaded with async), check document.readyState and run immediately if it is not loading. Also place scripts at the end of the body only for old setups; defer in the head is better because the download starts early.
const events = [];
const record = (name) => events.push(`${name} (readyState: ${document.readyState})`);
record('script starts');
document.addEventListener('readystatechange', () => record('readystatechange'));
document.addEventListener('DOMContentLoaded', () => record('DOMContentLoaded'));
window.addEventListener('load', () => record('load'));
// a helper that works whether the script runs before or after the document is ready
function onReady(callback) {
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', callback, { once: true });
else callback();
}
onReady(() => record('onReady callback'));
// a late script: registered after the document is already complete, so it must run immediately
setTimeout(() => {
onReady(() => record('late onReady callback runs at once'));
console.log(events.join('\n'));
}, 50);Written for a browser console or script tag; checked here with Node.js v24.22.1 and jsdom, two runs with identical output. Output: script starts (readyState: interactive) onReady callback (readyState: interactive) DOMContentLoaded (readyState: interactive) readystatechange (readyState: complete) load (readyState: complete) late onReady callback runs at once (readyState: complete)
Key points
- Plain script blocks parsing; defer runs in order after parsing; async runs when ready in any order.
- Module scripts are deferred by default and run once in strict mode.
- DOMContentLoaded waits for parsing and deferred scripts, not images; load waits for everything.
- readyState moves from loading to interactive to complete.
- Check readyState in scripts that may run late, and prefer defer in the head.
Common mistakes
- Putting a blocking script in the head without defer.
- Using async for scripts that depend on each other or on the DOM.
- Waiting for DOMContentLoaded in a script that may run after the event, so the callback never runs.
- Assuming DOMContentLoaded waits for images.
Follow-ups
- Why does a module script not need the defer attribute?
- Which event would you use to wait for images, and why is it usually too late?
- How do preload and modulepreload help loading performance?
Tips
- Describe the three timelines in one sentence each.
- Recommend defer for DOM-dependent scripts and async for independent ones.
- Show the readyState check for late-running code.