👀 Live Preview
A typical async script tag in HTML:
The browser downloads and runs the script without blocking HTML parsing.

The async attribute is a valuable feature in HTML that is specifically used with script elements. It controls whether the associated script should be executed asynchronously or not. This attribute can significantly impact the loading and execution behavior of scripts in your web page.
Parallel download.
Present = true.
Requires src URL.
Know the difference.
script.async = true
Faster page loads.
asyncThe primary purpose of the async attribute is to influence the way scripts are loaded and executed. When the async attribute is present, it allows the script to be downloaded asynchronously, meaning it won’t block the parsing of the rest of the page. This is particularly useful for non-essential scripts that don’t depend on the page structure.
async applies to <script src="..."> elements. It does not affect inline scripts without a src attribute.
Add the boolean async attribute to an external script tag:
<script async src="myscript.js"></script>async alone; presence means true.src attribute pointing to an external file.async and defer on the same script (defer is ignored if async is present).The async attribute has a boolean value, and it can be set to either:
async attribute.defer is used).<!-- Enabled (recommended HTML syntax) -->
<script async src="analytics.js"></script>
<!-- Also valid but redundant -->
<script async="async" src="analytics.js"></script>
<!-- Default: no async -->
<script src="app.js"></script>| Loading Mode | Download | Execute | Order |
|---|---|---|---|
async | Parallel | When ready | Not guaranteed |
defer | Parallel | After HTML parsed | Preserved |
| (default) | Blocks parsing | Immediately | Preserved |
| Best for | Analytics, ads, independent widgets | ||
| Applicable element | <script src="..."> | ||
| Element | Supported? | Notes |
|---|---|---|
<script src="..."> | Yes | Primary and only standard use |
<script> (inline) | No effect | async ignored without src |
<link> | No | Use preload hints instead |
<img> | No | Use loading="lazy" for images |
Basic async scripts, dynamic JavaScript loading, and async vs defer comparison.
A typical async script tag in HTML:
The browser downloads and runs the script without blocking HTML parsing.
Here’s a simple example of how to use the async attribute in an HTML script element:
<script async src="myscript.js"></script>In this example, the async attribute indicates that the script myscript.js should be loaded and executed asynchronously. HTML parsing continues while the file downloads.
Similar to other HTML attributes, you can also dynamically set the async attribute using JavaScript. This can be useful when you need to change the loading behavior of scripts based on certain conditions or user interactions. Here’s a brief example:
<script>
var scriptElement = document.createElement("script");
scriptElement.src = "dynamicScript.js";
scriptElement.async = true;
document.head.appendChild(scriptElement);
</script>In this script, a new script element is created dynamically, and the async property is set to true before appending it to the head of the document. This allows dynamicScript.js to be loaded asynchronously.
Choose the right loading strategy for your scripts:
<!-- Independent analytics: async -->
<script async src="analytics.js"></script>
<!-- App logic that needs DOM: defer -->
<script defer src="app.js"></script>Use async for standalone scripts that do not depend on other scripts or DOM readiness. Use defer when scripts must run in order after the document is parsed.
Parser finds <script async src="...">.
HTML parsing continues while the file fetches.
Parsing may pause briefly during execution.
The page becomes interactive sooner while independent scripts load.
The async attribute is supported in all modern browsers on external script elements.
All major browsers honor async on <script src> elements.
Bottom line: Use async confidently on external scripts in all modern browsers.
async for independent scripts (analytics, ads)src attributedefer when scripts depend on each otherasync on scripts that must run in a specific orderasync to inline scripts expecting an effectasync and defer on the same tagasyncThe async attribute is a powerful tool for optimizing the loading and execution of scripts in HTML. By understanding how and when to use this attribute, you can improve the performance and user experience of your web pages.
Reach for async when scripts are self-contained. When order or DOM readiness matters, choose defer or place scripts at the end of the body instead.
asyncBookmark these before adding scripts to your page.
Parallel download.
SpeedPresence = true.
SyntaxExternal only.
ScopeOrder differs.
CompareDynamic load.
JS<script> element with an external src attribute.async runs when ready (order not guaranteed). defer waits until HTML is parsed and keeps script order.src attribute are affected.scriptElement.async = true on dynamically created script elements.Practice the async attribute with external and dynamic script examples in the Try It editor.
5 people found this page helpful