Tuesday, 1 Jul 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML

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

    By Chief Editor

    Difference between HTML Tag and HTML Element in 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
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor
  • JavaScript

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

    By Chief Editor

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

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

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor
  • Frontend Interview

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor
  • Backend Interview

    Difference between display none and visibility hidden in CSS?

    By Chief Editor

    What is Flex Box in CSS?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    How to Improve the Performance of React Applications

    By Chief Editor

    How can you delay the dispatch in React?

    By Chief Editor

    System Design and Frontend System Design: An In-depth Overview

    By Chief Editor
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • 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

Lazy Loading in React.js: Boosting Performance and Reducing Load Time

In modern web development, performance plays a crucial role in user experience. One technique that…

By Chief Editor

Explain var let and const in JavaScript with Example.

In JavaScript, var, let, and const are used to declare variables. They differ in scope,…

By Chief Editor

How Can You Share Data Between Components in React?

In React, there are several ways to share data between components, depending on the relationship…

By Chief Editor

You Might Also Like

What is Symentic HTML
Frontend InterviewHTML

What is Symentic HTML?

By Chief Editor
HTML

What is Doctype HTML in HTML?

By Chief Editor
Frontend InterviewHTML

What is Block level Element and Inline Level Element?

By Chief Editor
HTML

What is a Meta Tag in HTML?

By Chief Editor

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
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?