Deferred scripts improve load performance by removing parser blocking while preserving execution order. The real pitfall is assuming defer behaves like async or running DOM-dependent code without understanding script timing.
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
What is the role of deferred scripts in JavaScript?Frontend interview answer
This JavaScript interview question tests whether you can explain JavaScript defer in production: render-blocking, execution order, and async vs defer pitfalls, connect it to production trade-offs, and handle common follow-up questions.
- JavaScript defer in production: render-blocking, execution order, and async vs defer pitfalls explanation without falling back to memorized docs wording
- Script Loading and Performance reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
Performance pitfall
A normal <script> tag blocks HTML parsing. defer changes that by downloading during parsing and executing after the document is parsed, in order.
The production bug appears when teams confuse defer with async or rely on DOM timing without understanding how script execution order really works.
<!-- Regular script (blocks rendering) -->
<script src="main.js"></script>
<!-- Deferred script (non-blocking) -->
<script src="main.js" defer></script>
Attribute | When It Executes | HTML Parsing | Use Case |
|---|---|---|---|
No Attribute | Immediately, as soon as it's downloaded. | Blocks HTML parsing until execution finishes. | Small scripts or inline JS that must run early. |
defer | After HTML parsing completes (in order). | Does not block HTML parsing. | Scripts that depend on the DOM being ready. |
async | As soon as it's downloaded (not ordered). | Does not block HTML parsing, but order is unpredictable. | Independent scripts like analytics or ads. |
How Deferred Scripts Work Internally
- The browser encounters a
<script defer>tag while parsing HTML.
- It starts downloading the script in parallel with HTML parsing.
- The script is queued for execution after the document has been fully parsed.
- Deferred scripts preserve their order — if multiple scripts are marked as
defer, they will execute sequentially in the order they appear in the document.
This makes defer especially useful for including multiple dependent scripts without blocking page rendering.
<!-- Both scripts load in parallel but run after HTML parsing -->
<script src="lib.js" defer></script>
<script src="app.js" defer></script>
<!-- Guaranteed: lib.js runs before app.js -->
Why Deferred Scripts Are Important
- They improve performance by eliminating render-blocking delays.
- The DOM is fully available when they execute, meaning no need to wait for
DOMContentLoadedmanually.
- Execution order is preserved, unlike with
async.
- They are ideal for application scripts that depend on the DOM or other scripts.
Deferred scripts effectively allow your HTML, CSS, and JS to load concurrently without competing for render time.
// main.js (loaded with defer)
console.log(document.readyState); // 'interactive' or 'complete'
window.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully parsed before script ran!');
});
Key Considerations
- The
deferattribute only works for external scripts (those withsrcattributes).
- Inline scripts cannot use
defer.
- In old browsers (pre-HTML5), support for
deferwas inconsistent, but it’s now standard in all major browsers.
- Combining
deferandasyncon the same script is invalid — only one behavior applies.
Think of defer like scheduling a meeting after you've finished your main work — the script is downloaded while you’re busy, but it only executes once your workspace (HTML) is ready. It keeps everything efficient and orderly.
deferallows scripts to load without blocking HTML parsing.
- Deferred scripts execute only after the DOM is fully parsed.
- Execution order is maintained as per their order in the document.
- Perfect for DOM-dependent scripts or modular JavaScript bundles.
- Leads to faster, smoother user experiences and better performance scores (like in Lighthouse).
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.