FrontendDeveloper.in

JavaScript question detail

What are the different ways to execute external scripts?

There are three different ways to execute external scripts,

  1. async: The script is downloaded in parallel to parsing the page, and executed as soon as it is available even before parsing completes. The parsing of the page is going to be interuppted once the script is downloaded completely and then the script is executed. Thereafter, the parsing of the remaining page will continue.

The syntax for async usage is as shown below,

<script src="demo.js" async></script>
  1. defer: The script is downloaded in parallel to parsing the page, and executed after the page has finished parsing.

The syntax for defer usage is as shown below,

<script src="demo.js" defer></script>
  1. Neither async or defer: The script is downloaded and executed immediately by blocking parsing of the page until the script execution is completed.

Note: You should only use either async or defer attribute if the src attribute is present.

Back to all JavaScript questions
Get LinkedIn Premium at Rs 399