diff --git a/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg b/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg new file mode 100644 index 000000000000000..639914362df0683 Binary files /dev/null and b/files/en-us/learn/javascript/first_steps/what_is_javascript/async-defer.jpg differ diff --git a/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html b/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html index c46d425f0ca3d00..86d624ece535f97 100644 --- a/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html +++ b/files/en-us/learn/javascript/first_steps/what_is_javascript/index.html @@ -335,7 +335,19 @@

async and defer

There are actually two modern features we can use to bypass the problem of the blocking script — async and defer (which we saw above). Let's look at the difference between these two.

-

Scripts loaded using the async attribute (see below) will download the script without blocking rendering the page and will execute it as soon as the script finishes downloading. You get no guarantee that scripts will run in any specific order, only that they will not stop the rest of the page from displaying. It is best to use async when the scripts in the page run independently from each other and depend on no other script on the page.

+

Scripts loaded using the async attribute will download the script without blocking the page while the script is being fetched. However, once the download is complete, the script will execute, which blocks the page from rendering. You get no guarantee that scripts will run in any specific order. It is best to use async when the scripts in the page run independently from each other and depend on no other script on the page.

+ +

Scripts loaded with the defer attribute will load in the order they appear on the page. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).

+ +

Here is a visual representation of the different script loading methods and what that means for your page:

+ +
+ async vs defer +
Image from the HTML spec, copied and cropped to a reduced version, under CC BY 4.0 license terms. +
+
+ +

For example, if you have the following script elements:

@@ -357,12 +369,14 @@

async and defer

<script defer src="js/script3.js"></script> -

All the scripts with the defer attribute will load in the order they appear on the page. So in the second example, we can be sure that jquery.js will load before script2.js and script3.js and that script2.js will load before script3.js. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).

+

In the second example, we can be sure that jquery.js will load before script2.js and script3.js and that script2.js will load before script3.js. They won't run until the page content has all loaded, which is useful if your scripts depend on the DOM being in place (e.g. they modify one of more elements on the page).

To summarize: