Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

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

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor
  • JavaScript

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

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    What is a Closure in JavaScript?

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What is a Promise in JavaScript, and what are its parameters?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Position in CSS?

    By Chief Editor
  • Backend Interview

    What is localization in React?

    By Chief Editor

    What is NodeJS and its main features?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What is a Function Component in React?

    By Chief Editor

    What is Redux, and how does it work?

    By Chief Editor

    What is props drilling in React?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog 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 are the async and defer attributes in the “script” tag?

What are the async and defer Attributes in the <script> Tag? When loading JavaScript in…

By Chief Editor

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

Difference Between document.createElement() and document.createDocumentFragment() in JavaScript 1. document.createElement() This method creates a single HTML…

By Chief Editor

You Might Also Like

HTML

Difference between HTML Tag and HTML Element in HTML?

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 are the different types of HTML tags?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?