Sobes.tech
Middle

What is the difference between <script>, <script async>, and <script defer> tags?

sobes.tech AI

Answer from AI

  • <script>: HTML parsing stops, the script loads and executes, then HTML parsing resumes. It blocks rendering.
  • <script async>: The script loads asynchronously, parallel to HTML parsing. As soon as the script is loaded, HTML parsing is interrupted to execute the script, then resumes. The execution order of asynchronous scripts is not guaranteed.
  • <script defer>: The script loads asynchronously, parallel to HTML parsing. Execution is deferred until HTML parsing and DOM construction are complete. The order of deferred scripts is preserved according to their order in the document.

Here is a table summarizing the differences:

Attribute Loading Execution Blocks HTML parsing Script order
absent Synchronous Immediately after loading Yes Guaranteed
async Asynchronous Immediately after loading (if HTML is still parsing) Yes (during execution) Not guaranteed
defer Asynchronous After full HTML loading and DOM construction No (except during execution, but after HTML) Guaranteed