Understanding the Magic of Async and Defer Attributes in HTML

Niket Kumar Mishra
3 min readOct 22, 2023

--

Hello Reader 👋, Welcome in the new tech blog about async & defer in HTML. If you are a frontend web developer or want to become I am sure you heard many times about async & defer, so If you want to understand it read this article, I have Explained this in very simple manner.

In the realm of web development, where speed and efficiency matters a lot, the optimal loading of web pages can make a big difference in user experience. Looking for fast loading times and seamless user interactions,, HTML provides developers with two powerful attributes: async and defer. Understanding these attributes and their impact on the loading behavior of scripts can significantly enhance the performance of web applications. Let’s dive deeper into the world of async and defer attributes in HTML and highlight their benefits.

First of all let’s understand how normal script tag in html work.

Script Tag

When a browser encounters a standard script tag, it typically blocks the parsing of the HTML document until the script has been fully downloaded and executed. This means that if a script tag is placed in the head of an HTML document, the rendering of the page will pause until the script has been completely loaded, leading to potential delays in the display of content for the user. Similarly, if the script tag is placed at the end of the body, other elements on the page may not load until the script has been fetched and executed.

<script src="./main.js"></script>
script-niket
Script tag behavior in HTML

The Async Attribute

The async attribute is employed to load and execute scripts asynchronously, ensuring that the script doesn't block the rendering of the page. When the browser encounters a script with the async attribute, it initiates the fetching process immediately, allowing the HTML parsing and rendering to continue in parallel. As a result, the script may execute at an unpredictable time, potentially before the entire document has been parsed. While this approach boosts page load speed, it can lead to issues if the script's execution depends on the availability of certain elements on the page.

<script async src="./main.js"></script>
async-script-niket
Script tag behavior with async attribute in HTML

The Defer Attribute

On the other hand, the defer attribute is utilized to load scripts in a non-blocking manner while maintaining the order of execution. When a script is tagged with the defer attribute, the browser initiates the fetching process but delays the execution until the HTML parsing is complete. This ensures that the script executes only after the entire document has been parsed, enabling it to access and manipulate the complete DOM. The defer attribute is particularly useful for scripts that rely on the complete DOM structure to function correctly.

<script defer src="./main.js"></script>
defer-script-niket
Script tag behavior with defer attribute in HTML

Conclusion

In the ever-evolving landscape of web development, the async and defer attributes in HTML stand as important tools to optimizing the loading behavior of scripts. By judiciously implementing these attributes and considering the specific requirements of each script, developers can significantly enhance the performance and user experience of their web applications. Balancing speed with functionality, the thoughtful use of async and defer attributes can pave the way for a more seamless and engaging web browsing experience.

--

--

Niket Kumar Mishra

A dedicated frontend web developer and MERN Stack enthusiast with a passion for tech exploration and sharing knowledge through insightful tech blogs.