[Easy] π <script>, <script async>, <script defer>
Overviewβ
In HTML, there are three primary ways to load JavaScript files:
<script><script async><script defer>
These three methods behave differently during script loading and execution.
Detailed Comparisonβ
<script>β
- Behavior: When the browser encounters this tag, it pauses HTML parsing, downloads and executes the script, then continues parsing.
- When to use: For scripts that are critical to rendering.
- Pros: Ensures scripts execute in order.
- Cons: Can delay page rendering.
<script src="important.js"></script>
<script async>β
- Behavior: The browser downloads the script in the background while continuing to parse HTML. Once downloaded, the script executes immediately and may interrupt parsing.
- When to use: For independent scripts such as analytics or ads.
- Pros: Does not block HTML parsing and can improve loading speed.
- Cons: Execution order is not guaranteed and the script may run before the DOM is fully ready.
<script async src="analytics.js"></script>
<script defer>β
- Behavior: The browser downloads the script in the background but executes it only after HTML parsing completes. Multiple deferred scripts execute in document order.
- When to use: For scripts that need a complete DOM but are not needed immediately.
- Pros: Does not block HTML parsing, preserves order, and works well for DOM-dependent scripts.
- Cons: If the script is critical, it may delay interactivity.
<script defer src="ui-enhancements.js"></script>
Analogyβ
Imagine you are preparing for a date:
-
<script>οΌ You stop everything and call your partner to confirm details. Communication is clear, but preparation is delayed. -
<script async>οΌ You prepare while talking through Bluetooth earbuds. It's efficient, but you might lose focus and make mistakes. -
<script defer>οΌ You send a message saying you'll call after getting ready. You can finish preparation first, then communicate properly.
Current Usageβ
In modern frameworks, you usually do not configure <script> behavior manually.
For example, Vite defaults to type="module", which behaves similarly to defer.
<script type="module" src="/src/main.js"></script>
Exceptions are third-party scripts, such as Google Analytics.
<script async src="https://www.google-analytics.com/analytics.js"></script>