DEV Community

Cover image for Understanding `async` and `defer` in JavaScript: A Guide for Interview Preparation
Nishant Singh
Nishant Singh

Posted on • Edited on

Understanding `async` and `defer` in JavaScript: A Guide for Interview Preparation

JavaScript is a language that runs in the browser to enhance user experience by manipulating the Document Object Model (DOM), handling events, and performing asynchronous operations. When a web page is loaded, the browser parses the HTML and executes the JavaScript in the order it encounters the <script> tags. However, JavaScript execution can block the parsing of HTML, potentially slowing down the page load time. To optimize this, modern web development uses async and defer attributes on <script> tags, especially when loading external scripts.

This article will delve into what async and defer are, how they work, and their key differences—knowledge that is crucial for any JavaScript developer, particularly in interviews.

Legend

1. The Default Behavior of Script Tags

default

Before we explore async and defer, it’s essential to understand the default behavior of <script> tags:

  • Blocking Behavior: When the browser encounters a <script> tag, it stops parsing the HTML, downloads the script, and executes it immediately before continuing with the rest of the HTML.

  • Order of Execution: If there are multiple script tags, they are executed in the order they appear in the HTML document. This can lead to slower page loads, as the browser waits for each script to download and execute before moving on.

2. The async Attribute

The async attribute is used to load scripts asynchronously, which means the browser doesn’t block the HTML parsing while the script is being downloaded. However, the script will execute immediately after it has been downloaded, potentially out of order relative to other scripts.

async

Key Points:

  • Non-blocking Download: The script is downloaded while the HTML continues to be parsed.
  • Execution Timing: The script is executed as soon as it is downloaded, without waiting for the rest of the HTML to be parsed.
  • Order of Execution: Scripts with the async attribute may execute in any order, depending on which script finishes downloading first.

Example:



<script async src="script1.js"></script>
<script async src="script2.js"></script>


Enter fullscreen mode Exit fullscreen mode

In this case, script1.js and script2.js are downloaded concurrently. Whichever script finishes downloading first will execute first, potentially leading to an unpredictable order of execution.

When to Use async:

  • For independent scripts that don’t rely on each other or the DOM being fully parsed (e.g., analytics or advertisement scripts).

3. The defer Attribute

The defer attribute also loads scripts asynchronously, but it ensures that the scripts are executed in the order they appear in the document. Additionally, scripts with the defer attribute will only execute after the HTML parsing is complete.

defer

Key Points:

  • Non-blocking Download: Like async, the script is downloaded while the HTML continues to be parsed.
  • Execution Timing: The script is executed after the HTML document has been fully parsed.
  • Order of Execution: Scripts with the defer attribute execute in the order they appear in the document.

Example:



<script defer src="script1.js"></script>
<script defer src="script2.js"></script>


Enter fullscreen mode Exit fullscreen mode

Here, script1.js and script2.js are downloaded concurrently, but they will execute in the order they appear, only after the HTML parsing is complete.

When to Use defer:

  • For scripts that rely on the DOM being fully loaded or that need to be executed in a specific order (e.g., main application scripts).

4. Comparison of async and defer

Attribute HTML Parsing Script Download Script Execution Execution Order
async Continues Asynchronously Immediately after download Unpredictable, as per download order
defer Continues Asynchronously After HTML parsing is complete Preserved as per document order

async defer

5. Common Interview Questions

When it comes to interviews, understanding async and defer is essential. Here are some common questions you might encounter:

  • What is the difference between async and defer attributes in a script tag?

    • async executes the script as soon as it's downloaded, while defer waits until the entire HTML document is parsed.
  • When would you use async over defer and vice versa?

    • Use async for independent scripts that don’t depend on other scripts or the DOM. Use defer for scripts that need the DOM to be fully parsed or that depend on other scripts.
  • What are the potential issues with using async?

    • Since async scripts may execute in an unpredictable order, it can lead to issues if one script depends on another.

6. Conclusion

Understanding async and defer is crucial for optimizing the performance of web pages. By knowing when and how to use these attributes, you can ensure faster page loads and a better user experience. As a JavaScript developer, particularly in an interview setting, being able to articulate the differences and appropriate use cases for async and defer will demonstrate your proficiency in modern web development practices.

Make sure to experiment with these attributes in your projects to get a solid grasp of their behavior. Happy coding!

Top comments (0)