31. What is the difference between async and defer?

Both allow external scripts to download without blocking HTML parsing in the same way as a normal parser-blocking script. defer scripts execute after parsing and preserve document order. async scripts execute as soon as they are ready, so their execution order is not guaranteed relative to other async scripts.

Key Takeaways
  • Use defer when script order and post-parse execution matter.
  • Use async for independent scripts that do not rely on execution order.
Knowledge Check

Which preserves execution order among deferred classic scripts?