Deferred scripts are JavaScript files that load in parallel with HTML parsing but execute only after the document has been fully parsed. They help improve page load performance by preventing render-blocking behavior.
What is the role of deferred scripts in JavaScript?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
The Core Idea
Normally, when the browser encounters a <script> tag during HTML parsing, it stops rendering, fetches the script, and executes it immediately before continuing. This behavior blocks the page rendering process and slows down the perceived load time.
Deferred scripts solve this problem by allowing the browser to continue parsing HTML while downloading the script in the background — executing it only after the entire document is parsed.
<!-- 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).