← All questions
async vs defer on script tags | Web Fundamentals
EasyPremiumWeb FundamentalsWeb Fundamentals interview practice: Difference between async and defer attributes on script loading.
Web Fundamentals interview practice: Difference between async and defer attributes on script loading.
Both let the browser download the script without blocking HTML parsing. They differ in when the script executes:
| Download | Execute | Order preserved | DOM ready? | |
|---|---|---|---|---|
| (none) | blocking | immediately, blocks parsing | yes | maybe not |
async | parallel | as soon as downloaded (can interrupt parsing) | no (whoever finishes first) | not guaranteed |
defer | parallel | after HTML parsing, before DOMContentLoaded | yes (in order) | yes |
<script src="analytics.js" async></script> <!-- independent, order doesn't matter -->
<script src="framework.js" defer></script> <!-- needs DOM, order matters -->
<script src="app.js" defer></script>
Rule of thumb: use defer for scripts that depend on the DOM or on each other; use async for independent third-party scripts (analytics, ads).