🟒 EasyDOM & EventsπŸ“– Theory Question

What is the difference between async and defer for script loading?

πŸ’‘

Hint

Both avoid blocking HTML parsing β€” async executes ASAP, defer after full parse

Full Answer

By default, <script> blocks HTML parsing while downloading and executing.

AttributeDownloadExecutes whenOrder
None (default)Blocks HTMLImmediately, blocksIn order
asyncParallelAs soon as downloadedNOT guaranteed
deferParallelAfter HTML fully parsedIn document order
<!-- Blocks parsing ❌ (put in <head>) -->
<script src="app.js"></script>

<!-- Parallel download, executes ASAP when downloaded -->
<!-- ORDER NOT GUARANTEED β€” analytics.js might run before vendor.js -->
<script async src="analytics.js"></script>

<!-- Parallel download, runs after HTML parsed, IN ORDER βœ… -->
<script defer src="vendor.js"></script>
<script defer src="app.js"></script>
<!-- app.js always runs after vendor.js -->

When to use:

  • defer β€” most app scripts (DOM-dependent, order-dependent)
  • async β€” completely independent scripts (analytics, ads)
πŸ’‘ type="module" scripts are deferred by default. In modern apps using bundlers, you usually put one deferred script tag pointing at the bundle.

More DOM & Events Questions

🟒 EasyExplain event delegation and why it is useful.β†’πŸŸ’ EasyWhat is the difference between event.stopPropagation() and event.preventDefault()?β†’πŸŸ’ EasyWhat is the difference between event bubbling and event capturing?β†’πŸŸ’ EasyHow do you create and dispatch Custom Events?β†’

Practice this in a timed sprint β†’

5 free questions, no signup required

⚑ Start Sprint