Thursday, 16 Oct 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    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 the difference between “HTML” and “HTML5”?

    By Chief Editor
  • JavaScript
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    What is hoisting in JavaScript with an example?

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor
  • Frontend Interview

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    What is memoization in JavaScript?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Mastering Star Rating Systems: Best Practices and Optimized Code Examples

    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
  • Frontend Interview
  • HTML
  • CSS
  • Redux
  • Backend Interview
  • NodeJS
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

How to Reverse a String in JavaScript: Two Essential Methods

Reversing a string is one of the most common beginner-level tasks in programming. It’s a…

By Chief Editor

What are the Lexical Scope in JavaScript?

Lexical scope in JavaScript means that the scope of a variable is determined by its…

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

HTML

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

By Chief Editor
HTML

What are the different types of HTML tags?

By Chief Editor
Frontend InterviewHTML

What is Block level Element and Inline Level Element?

By Chief Editor
HTML

What is Doctype HTML in HTML?

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?