Friday, 18 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • Home
  • HTML

    What are the async and defer attributes in the “script” tag?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
  • JavaScript

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What is memoization in JavaScript?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    Difference between document.createElement and document.createElementFragement in JavaScript?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Frontend Interview

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What are the map, filter, and reduce methods in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    How can you delay the dispatch in React?

    By Chief Editor

    What is Virtual DOM in React?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home What are the async and defer attributes in the “script” tag?

HTML

What are the async and defer attributes in the “script” tag?

Chief Editor
Last updated: February 16, 2025 1:38 pm
Chief Editor
Share
SHARE

What are the async and defer Attributes in the <script> Tag?

When loading JavaScript in an HTML document using the <script> tag, the async and defer attributes control how the script is downloaded and executed.

Contents
1. Default Behavior (Without async or defer):2. async Attribute:3. defer Attribute:4. Comparison Table:5. Which One Should You Use?6. Example Usage:

1. Default Behavior (Without async or defer):

htmlCopyEdit<script src="script.js"></script>
  • The script is downloaded and executed immediately when the browser encounters it.
  • HTML parsing is paused until the script is fully loaded and executed.
  • Can block page rendering if the script is large or slow to load.

2. async Attribute:

htmlCopyEdit<script src="script.js" async></script>

How async Works:

  • Script is downloaded in parallel with HTML parsing.
  • Once downloaded, the script is executed immediately, pausing HTML parsing.
  • Scripts with async are executed as soon as they are ready, regardless of their order in the document.

Best For:

  • Independent scripts that don’t depend on other scripts or DOM content.
  • Tracking scripts, analytics, or ads.

Key Points:

✅ Non-blocking download.
⏸️ Blocking execution (pauses HTML parsing when it runs).
❌ Execution order is not guaranteed (e.g., if multiple async scripts are present).


3. defer Attribute:

htmlCopyEdit<script src="script.js" defer></script>

How defer Works:

  • Script is downloaded in parallel with HTML parsing.
  • Execution is deferred until after the entire HTML document is parsed.
  • Scripts with defer are executed in the order they appear in the document.

Best For:

  • Scripts that depend on the DOM being fully loaded.
  • When you have multiple scripts that should run in sequence.

Key Points:

✅ Non-blocking download.
✅ Non-blocking execution (executed after the HTML parsing is done).
✅ Execution order is guaranteed.


4. Comparison Table:

AttributeDownload TimeExecution TimeBlocks HTML Parsing?Execution Order
NoneWhen encounteredImmediately after downloadYesIn document order
asyncParallel with HTML parsingImmediately when readyYes (pauses parsing)Random (depends on download speed)
deferParallel with HTML parsingAfter HTML is fully parsedNoIn document order

5. Which One Should You Use?

✅ Use defer for most scripts that rely on DOM content (e.g., application logic).
✅ Use async for scripts that don’t depend on other scripts or DOM (e.g., analytics, ads).
❌ Avoid blocking scripts (no async or defer) unless necessary.


6. Example Usage:

htmlCopyEdit<!-- Async: Good for Analytics -->
<script src="analytics.js" async></script>

<!-- Defer: Good for Application Logic -->
<script src="app.js" defer></script>
<script src="helper.js" defer></script>

Using defer is generally the safest and most efficient choice for scripts that manipulate the DOM or depend on other scripts.

Share This Article
Email Copy Link Print
Previous Article Is JavaScript a synchronous or asynchronous language?
Next Article What are the Lexical Scope in JavaScript What are the Lexical Scope in JavaScript?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

Difference Between position: relative and position: absolute in CSS

Propertyposition: relativeposition: absoluteDefinitionThe element is positioned relative to its original position in the normal document…

By Chief Editor

What is Arrow and Normal Function in JavaScript?

Arrow Function vs Normal Function in JavaScript In JavaScript, Arrow Functions and Normal Functions (also…

By Chief Editor

What are Benefits in React?

What are the Benefits of Using React? React is a popular JavaScript library for building…

By Chief Editor

You Might Also Like

Frontend InterviewHTML

What is Block level Element and Inline Level Element?

By Chief Editor
HTML

What is Doctype HTML in HTML?

By Chief Editor
HTML

What is the difference between “HTML” and “HTML5”?

By Chief Editor
HTML

What is a Meta Tag in HTML?

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?