Explain Asynchronous vs Deferred JavaScript Last Updated : 06 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Generally, when we use a script tag to load any JavaScript code, the HTML parsing is paused by the browser when it encounters the script tag and it starts to download the JavaScript file first. The HTML elements script tag will not be executed until the browser is done with downloading the script and executing it. The browser waits till the script gets downloaded, and executed, and after this, it processes the rest of the page. In modern browsers, the script tends to be larger than the HTML files, hence their download size is large and processing time will be longer. This increases the load time of the page and restricts the user to navigate through the website. To solve this problem, async and defer attributes come into play. Syntax: A normal script is included on the page in the following way. <script src = "script.js"></script> When the HTML parser finds this element, a request is sent to the server to get the script. Asynchronous: When we use the async attribute the script is downloaded asynchronously with the rest of the page without pausing the HTML parsing and the contents of the page are processed and displayed. Once the script is downloaded, the HTML parsing will be paused and the script's execution will happen. Once the execution is done, HTML parsing will then resume. The page and other scripts don't wait for async scripts and async scripts also don't wait for them. It is great for independent scripts and externally located scripts. Syntax: <script async src = "script.js"></script> Deferred: The defer attribute tells the browser not to interfere with the HTML parsing and only to execute the script file once the HTML document has been fully parsed. Whenever a script with this attribute is encountered, the downloading of the script starts asynchronously in the background and when the scripts get downloaded, it is executed only after the HTML parsing is finished. Syntax: <script defer src = "script.js"></script> Asynchronous vs Deferred: AsynchronousDeferredAsynchronous blocks the parsing of the page.Deferred never blocks the page.Asynchronous scripts don't wait for each other. So if a smaller script is second in the order, it will be loaded before the previous longer one.Deferred scripts maintain their relative order which means the first script will be loaded first while all others below it will have to wait.The execution of scripts begins by pause parsing.However, the execution of scripts begins only after parsing is completely finished but before the document's DOMContentLoadedevent. Comment More infoAdvertise with us Next Article Explain Asynchronous vs Deferred JavaScript S shubhamkumarlhh Follow Improve Article Tags : JavaScript Web Technologies javascript-basics Similar Reads Asynchronous JavaScript Asynchronous JavaScript is a programming approach that enables the non-blocking execution of tasks, allowing concurrent operations, improved responsiveness, and efficient handling of time-consuming operations in web applications, JavaScript is a single-threaded and synchronous language. The code is 2 min read JavaScript - How Does Asynchronous Code Work? Asynchronous code in JavaScript allows to execute code in the background without blocking the main thread. Asynchronous JavaScript is mainly used for handling tasks like network requests, file operations, and API calls. To understand how asynchronous code works, it's important to first know the conc 4 min read Synchronous and Asynchronous in JavaScript JavaScript is known for its ability to handle both synchronous and asynchronous operations. Understanding how these two things work is important for writing efficient, responsive, and user-friendly applications. In this article, we will see the differences between synchronous and asynchronous JavaSc 4 min read How asynchronous JavaScript code gets executed in browser ? Functions or operations that are running in parallel with the other functions or operations are called the asynchronous functions or operations in JavaScript. Asynchronous functions use Callback Functions that get executed later. Let us see an example that demonstrates this: JavaScript setTimeout(fu 3 min read How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read How to chain asynchronous functions in JavaScript ? JavaScript is a single-threaded, asynchronous programming language. Thus, some time-consuming operations like I/O, accessing the database, network calls, etc. are performed asynchronously so that it does not interrupt things in the only JS thread. This can be done by asynchronous code like promises 2 min read How to execute a script asynchronously in HTML5 ? In this article, we will learn how to execute a script asynchronously on a webpage. This can be used in situations where loading the content is important as heavy scripts can cause the page to wait for it to load thereby making the page appear incomplete. Asynchronously loading would solve this issu 3 min read Explain Promise.all with async-await in JavaScript In JavaScript, Promise.all with async-await is used to handle multiple asynchronous operations concurrently. By combining Promise.all with await, you can wait for all promises to resolve or any to reject, ensuring that all asynchronous tasks are complete before proceeding with the next code executio 4 min read Explain Promise.any() with async-await in JavaScript In this article, we will try to understand how to implement the Promise.any() method with async-await in JavaScript using some theoretical explanations followed by some coding examples as well. Let us firstly quickly understand the working of Promise.any() method with some theoretical examples (incl 4 min read How To Return the Response From an Asynchronous Call in JavaScript? To return the response from an asynchronous call in JavaScript, it's important to understand how JavaScript handles asynchronous operations like fetching data, reading files, or executing time-based actions. JavaScript is a single-threaded nature means it can only handle one task at a time, but it u 3 min read Like