{"id":5421,"date":"2025-05-01T07:32:26","date_gmt":"2025-05-01T07:32:26","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=5421"},"modified":"2025-05-01T07:32:26","modified_gmt":"2025-05-01T07:32:26","slug":"async-vs-defer-in-javascript-explained","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/async-vs-defer-in-javascript-explained\/","title":{"rendered":"Async vs Defer in JavaScript Explained"},"content":{"rendered":"<h1>Understanding Async vs Defer in JavaScript: An In-Depth Guide<\/h1>\n<p>As web applications grow more complex, the importance of optimizing the loading speed and performance of your JavaScript becomes clear. Two powerful attributes introduced in HTML5 help achieve this: <strong>async<\/strong> and <strong>defer<\/strong>. In this article, we will explore these attributes in detail, their differences, use cases, and provide examples to illustrate how each affects page loading.<\/p>\n<h2>What are Async and Defer?<\/h2>\n<p>When including external JavaScript files, browsers can block HTML parsing to download and execute these scripts. This can lead to slower page load speeds and a poor user experience. The <strong>async<\/strong> and <strong>defer<\/strong> attributes offer a way to control this behavior effectively.<\/p>\n<p>Both attributes can be added to the <code>&lt;script&gt;<\/code> tag and essentially tell the browser how and when to execute the script after it has finished downloading:<\/p>\n<pre><code>&lt;script src=\"script.js\" async&gt;&lt;\/script&gt;<\/code><\/pre>\n<pre><code>&lt;script src=\"script.js\" defer&gt;&lt;\/script&gt;<\/code><\/pre>\n<h2>The <strong>Async<\/strong> Attribute<\/h2>\n<p>When you use the <strong>async<\/strong> attribute, the script is downloaded asynchronously while the HTML document continues to be parsed. As soon as the script is downloaded, it will interrupt the parsing to execute.<\/p>\n<h3>Behavior of Async<\/h3>\n<ul>\n<li>The script starts downloading immediately after the element is encountered.<\/li>\n<li>As soon as it finishes downloading, it executes immediately, even if the document hasn&#8217;t finished parsing.<\/li>\n<li>Scripts are executed in the order they finish downloading, not in the order they appear in the HTML.<\/li>\n<\/ul>\n<h3>Use Case for Async<\/h3>\n<p>Use the <strong>async<\/strong> attribute when the script is independent of any other scripts or any DOM manipulation. A common example is analytics scripts where the loading time isn&#8217;t affected by the order of execution:<\/p>\n<pre><code>&lt;script src=\"https:\/\/analytics.example.com\/script.js\" async&gt;&lt;\/script&gt;<\/code><\/pre>\n<h3>Example: Async in Action<\/h3>\n<p>The following example shows how an async script can be included in an HTML document:<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Async Example&lt;\/title&gt;\n    &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\" async&gt;&lt;\/script&gt;\n    &lt;script&gt;\n        \/\/ This won't wait for jQuery\n        console.log(\"This runs even if jQuery is not loaded.\");\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Async Example&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>The <strong>Defer<\/strong> Attribute<\/h2>\n<p>The <strong>defer<\/strong> attribute, on the other hand, tells the browser to continue parsing the HTML document and execute the script only after the document has been fully parsed. This ensures that the execution order of scripts is guaranteed to follow the order they appear in the HTML.<\/p>\n<h3>Behavior of Defer<\/h3>\n<ul>\n    &lt;liThe download begins when the script element is encountered.<\/li>\n<li>The script execution is postponed until after the entire HTML document has been parsed.<\/li>\n<li>Scripts are executed in the order they appear in the HTML, which is crucial for dependent scripts.<\/li>\n<\/ul>\n<h3>Use Case for Defer<\/h3>\n<p>The <strong>defer<\/strong> attribute is ideal for scripts that depend on the DOM being fully loaded. For example, any script that manipulates the document structure should be deferred:<\/p>\n<pre><code>&lt;script src=\"https:\/\/example.com\/script.js\" defer&gt;&lt;\/script&gt;<\/code><\/pre>\n<h3>Example: Defer in Action<\/h3>\n<p>Check out this example using the <strong>defer<\/strong> attribute:<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Defer Example&lt;\/title&gt;\n    &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\" defer&gt;&lt;\/script&gt;\n    &lt;script defer&gt;\n        \/\/ This will wait for jQuery to load\n        $(document).ready(function() {\n            console.log(\"DOM is fully loaded, and jQuery is ready to use.\");\n        });\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Defer Example&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Async vs Defer: Key Differences<\/h2>\n<p>Now that we&#8217;ve defined both attributes, let\u2019s summarize their key differences:<\/p>\n<table>\n<thead>\n<tr>\n<th>Attribute<\/th>\n<th>Execution Timing<\/th>\n<th>Load Order<\/th>\n<th>Use Case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>async<\/td>\n<td>As soon as it&#8217;s downloaded<\/td>\n<td>In the order they finish downloading<\/td>\n<td>Independent scripts (like analytics)<\/td>\n<\/tr>\n<tr>\n<td>defer<\/td>\n<td>After the entire document is parsed<\/td>\n<td>In the order they appear in HTML<\/td>\n<td>Scripts that rely on DOM fully loaded<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Performance Considerations<\/h2>\n<p>Using <strong>async<\/strong> and <strong>defer<\/strong> can significantly improve performance, especially for large applications. However, it&#8217;s important to consider the compatibility of these attributes with various browsers:<\/p>\n<ul>\n<li>Both attributes are supported in modern browsers.<\/li>\n<li>Older browsers might not support <strong>defer<\/strong> with scripts that have a <code>src<\/code> attribute, but they will still execute inline scripts.<\/li>\n<li>Always make sure to test across all target browsers to ensure consistent behavior.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In conclusion, choosing between <strong>async<\/strong> and <strong>defer<\/strong> can make a significant difference in the performance of a web application. Understanding when to use each attribute is critical to optimizing load times and ensuring a smooth user experience. For analytics scripts or other independent scripts, use <strong>async<\/strong>. For scripts that rely on the DOM, always go with <strong>defer<\/strong>.<\/p>\n<p>With this knowledge in hand, you can make informed decisions about loading external JavaScript in your projects, ultimately leading to faster, more efficient web applications.<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Async vs Defer in JavaScript: An In-Depth Guide As web applications grow more complex, the importance of optimizing the loading speed and performance of your JavaScript becomes clear. Two powerful attributes introduced in HTML5 help achieve this: async and defer. In this article, we will explore these attributes in detail, their differences, use cases,<\/p>\n","protected":false},"author":103,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[172],"tags":[330],"class_list":{"0":"post-5421","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-javascript","7":"tag-javascript"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=5421"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5421\/revisions"}],"predecessor-version":[{"id":5422,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5421\/revisions\/5422"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=5421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=5421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=5421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}