midHTML5
Explain the 'defer' and 'async' attributes for <script> tags.
Updated Apr 28, 2026
Short answer
They control how scripts are fetched and executed to prevent render-blocking.
Deep explanation
async downloads the script while the HTML parses but pauses parsing to execute immediately once downloaded. defer downloads during parsing but waits until the document is fully parsed before executing (preserving script order).
Real-world example
Using async for third-party scripts (ads/trackers) and defer for your app's main logic to ensure the DOM is ready.
Common mistakes
- Using `async` for scripts that depend on each other (they might execute out of order).
Follow-up questions
- Which one is better for DOM manipulation?