<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sheng Hsu</title>
    <description>The latest articles on DEV Community by Sheng Hsu (@shenghsu).</description>
    <link>https://dev.to/shenghsu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110424%2F343f382f-ad96-4b4e-8ee6-0e50a583c300.png</url>
      <title>DEV Community: Sheng Hsu</title>
      <link>https://dev.to/shenghsu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shenghsu"/>
    <language>en</language>
    <item>
      <title>Async vs Defer in JavaScript</title>
      <dc:creator>Sheng Hsu</dc:creator>
      <pubDate>Thu, 29 Jun 2023 10:55:51 +0000</pubDate>
      <link>https://dev.to/shenghsu/async-vs-defer-in-javascript-5hik</link>
      <guid>https://dev.to/shenghsu/async-vs-defer-in-javascript-5hik</guid>
      <description>&lt;p&gt;In the world of web development, performance is key. One of the ways to improve the performance of your website is by controlling how your JavaScript files are loaded. &lt;/p&gt;

&lt;p&gt;This is where the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes come into play. These attributes, when used with the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag, can significantly impact the loading and execution of your JavaScript files.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does Async do?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;async&lt;/code&gt; attribute in JavaScript allows the browser to continue parsing the HTML document while a script is being downloaded. &lt;/p&gt;

&lt;p&gt;This means that the script is executed as soon as it is available, even if the HTML is not fully parsed. This can speed up the loading time of your page, but it also means that scripts may be executed out of order if they are loaded at different speeds.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does Defer do?
&lt;/h2&gt;

&lt;p&gt;On the other hand, the &lt;code&gt;defer&lt;/code&gt; attribute tells the browser to continue parsing the HTML document while the script is being downloaded, but to wait until the HTML is fully parsed before executing the script. &lt;/p&gt;

&lt;p&gt;This ensures that scripts are executed in the order they appear in the HTML document, which can be important if one script depends on another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which is better?
&lt;/h2&gt;

&lt;p&gt;The choice between &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; depends on the specific needs of your website. If your scripts are independent and do not rely on each other, &lt;code&gt;async&lt;/code&gt; can be a good choice because it allows scripts to execute as soon as they are available. &lt;/p&gt;

&lt;p&gt;However, if your scripts depend on each other, &lt;code&gt;defer&lt;/code&gt; may be a better choice because it ensures that scripts are executed in the correct order.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when you use Async and Defer together?
&lt;/h2&gt;

&lt;p&gt;Technically, you can use both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes on the same &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. However, this is not recommended. &lt;/p&gt;

&lt;p&gt;The reason is that &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; have different execution behaviors, and using them together can lead to inconsistent results across different browsers.&lt;/p&gt;

&lt;p&gt;According to the HTML specification, if both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; are present, the &lt;code&gt;async&lt;/code&gt; attribute takes precedence in modern browsers, while older browsers that do not support &lt;code&gt;async&lt;/code&gt; will fall back to &lt;code&gt;defer&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This can be useful if you want to use &lt;code&gt;async&lt;/code&gt; but also want to provide a fallback for older browsers. However, it's important to use this combination intentionally and understand the potential cross-browser implications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; are powerful tools for controlling how your JavaScript files are loaded and executed. &lt;/p&gt;

&lt;p&gt;They can help improve the performance of your website, but they should be used with care. Always consider the dependencies between your scripts and the potential cross-browser implications before deciding which attribute to use.&lt;/p&gt;

</description>
      <category>async</category>
      <category>defer</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
