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

A normal <script src="..."> stops the browser from reading the HTML until the script is downloaded and run. async and defer both download the script in the background, but they run it at different times.

Pointasyncdefer
DownloadIn the backgroundIn the background
RunsAs soon as downloaded, can interrupt the HTMLAfter the whole HTML is read
Order of scriptsNot guaranteedSame as in the page
Use forIndependent scripts, like analyticsScripts that need the page or each other
<script src="analytics.js" async></script>
<script src="app.js" defer></script>

For most of your own scripts, defer in the head is the best choice.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.