DEV Community

Syed Ammar
Syed Ammar

Posted on

When to use "async" and when "defer" in script tag?

The async and defer attributes in the <script> tag are used to optimize script loading and execution in HTML. Both attributes allow scripts to load without blocking the rendering of the page, but they have key differences in how they handle execution.


1. async Attribute

  • How it Works:

    • The script is loaded asynchronously, meaning it downloads in parallel with other resources (like CSS or images).
    • The script executes as soon as it is downloaded, which may happen before, during, or after the HTML parsing is complete.
  • Best for:

    • Independent scripts that do not rely on or interact with other scripts.
    • Examples:
    • Analytics scripts (e.g., Google Analytics).
    • Advertising or tracking scripts.
    • These scripts do not depend on the HTML or other scripts to run correctly.
  • Example:

  <script src="analytics.js" async></script>
Enter fullscreen mode Exit fullscreen mode
  • Use Case: When script execution timing does not need to follow the document parsing order or depend on other scripts.

2. defer Attribute

  • How it Works:

    • The script is downloaded in parallel with other resources, just like async.
    • The script is executed only after the entire HTML document has been parsed.
  • Best for:

    • Scripts that depend on the complete DOM structure being available.
    • Scripts that interact with or depend on other scripts in a specific order.
    • Examples:
    • Frameworks or libraries (e.g., jQuery, Angular).
    • Inline scripts that manipulate DOM elements.
  • Example:

  <script src="main.js" defer></script>
Enter fullscreen mode Exit fullscreen mode
  • Use Case: When the script should execute only after the HTML document is fully parsed and you need to maintain execution order among multiple scripts.

Key Differences Between async and defer

Attribute Script Loading Script Execution Order Guarantee
async Parallel to HTML parsing As soon as it’s downloaded No, execution is immediate
defer Parallel to HTML parsing After HTML parsing is complete Yes, scripts execute in order

Summary

  • Use async for:

    • Independent scripts that don’t depend on other scripts or the DOM.
    • Performance-critical tasks like analytics or advertising.
  • Use defer for:

    • Scripts that rely on the DOM being fully loaded.
    • Multiple scripts that must execute in a specific order.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video 🎥

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay