HTML Attributes
HTML <script> async Attribute
The async attribute lets an external script download in parallel and run as soon as it is ready. It is a boolean attribute that improves page loading performance.
The <script> async Attribute
async is a boolean attribute for the <script> element. It applies only to external scripts (those with a src). When present, the browser downloads the script in the background without pausing HTML parsing, then executes it immediately once the download finishes, which may briefly interrupt parsing at that moment.
Because async scripts run as soon as they arrive, their execution order is not guaranteed. Use it for independent scripts like analytics.
Syntax
Syntax: <script src="file.js" async></script>. Being boolean, async takes no value and is either present or absent. It has no effect on inline scripts.
<script src="analytics.js" async></script>Example
Run the example. The external script loads asynchronously and, once ready, writes a message into the page.
<!DOCTYPE html>
<html>
<body>
<h2>Async script demo</h2>
<p id="status">Page parsed. Waiting for the async script...</p>
<script
src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.3/dist/confetti.browser.min.js"
async
onload="document.getElementById('status').textContent = 'Async script loaded and ran!';">
</script>
</body>
</html>Attribute Values
| Value | Description |
|---|---|
| async | Download in parallel; execute as soon as ready, order not guaranteed |
| defer | Download in parallel; execute after parsing, in document order |
| (neither) | Download and execute immediately, blocking HTML parsing |
async scripts can run in any order and may execute before the DOM is fully parsed. Use async for independent scripts like analytics; use defer when execution order or a fully parsed DOM matters.
Key Takeaways
- async is a boolean attribute for external <script> elements.
- It downloads the script without blocking HTML parsing.
- Scripts execute as soon as they finish downloading, in no fixed order.
- Use defer instead when order or a ready DOM matters.
- It has no effect on inline scripts.
