---
title: "What is the difference between defer and async on script tags?"
description: "Both attributes download scripts without blocking the HTML parser. defer runs them in order once parsing finishes; async runs each one as soon as it arrives."
url: https://www.danny.engineering/answer/html/what-is-the-difference-between-defer-and-async-on-script-tags
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["HTML"]
---

# What is the difference between defer and async on script tags?

## The short answer


Both `defer` and `async` let the browser download a script while it keeps parsing the HTML. The difference is when the script runs. A `defer` script waits until the document has been parsed and runs in the order it appears. An `async` script runs as soon as it has downloaded, in whatever order the downloads finish. Use `defer` for your own application code and `async` for independent scripts such as analytics.


```html
<head>
  <!-- your app: runs in order, after parsing -->
  <script defer src="/js/vendor.js"></script>
  <script defer src="/js/app.js"></script>

  <!-- independent: runs whenever it is ready -->
  <script async src="https://example.com/analytics.js"></script>
</head>
```


## Why it happens


A plain `script` tag with a `src` is parser-blocking. When the browser reaches it, it stops building the page, downloads the file, runs it and only then carries on. That is why scripts used to go at the end of the body. Scripts block because they can call `document.write` or read the DOM that has been built so far, and the browser must respect that.


The two attributes tell the browser the script does not need that guarantee:


|              | Downloads      | Runs                                      | Order           |
| ------------ | -------------- | ----------------------------------------- | --------------- |
| no attribute | blocks parsing | immediately                               | document order  |
| `defer`      | in parallel    | after parsing, before `DOMContentLoaded`  | document order  |
| `async`      | in parallel    | as soon as downloaded, pausing the parser | first to arrive |


So `defer` behaves like putting the script at the end of the body, but the download starts much earlier because the browser sees the tag in the head. `async` is the fastest to run, but you cannot rely on the DOM being complete or on another script having run first.


## How to fix it


### Use defer for scripts that depend on each other or on the DOM


If `app.js` uses a library loaded by `vendor.js`, or queries elements on the page, `defer` keeps the order and guarantees the document is parsed. You do not need a `DOMContentLoaded` listener inside a deferred script; the DOM is already there.


```html
<script defer src="/js/vendor.js"></script>
<script defer src="/js/app.js"></script>
```


### Use async for scripts that stand alone


Analytics, ads and error reporting do not depend on your code and nothing depends on them. `async` lets them start as early as possible.


```html
<script async src="https://example.com/analytics.js"></script>
```


### Know that modules are deferred by default


A `script` with `type="module"` is deferred automatically, so adding `defer` does nothing. You can add `async` to a module script to run it as soon as it and its imports are ready.


```html
<script type="module" src="/js/main.js"></script>
<script type="module" async src="/js/widget.js"></script>
```


### Scripts added with JavaScript are async


A script element created with `document.createElement` and appended to the page behaves like `async`. Set `script.async = false` if you need several dynamically added scripts to run in order.


```javascript
const script = document.createElement('script');
script.src = '/js/chart.js';
script.async = false;
document.head.append(script);
```


## Common pitfalls

- Adding `defer` or `async` to an inline script. They only apply to scripts with a `src`, except `async` on inline module scripts.
- Using `async` for code that touches the DOM. It may run before the elements it needs exist.
- Using `async` on scripts that depend on each other. The order changes depending on network speed, so the bug comes and goes.
- Setting both attributes. Modern browsers treat the script as `async`; `defer` is only a fallback for very old ones.
- Listening for `DOMContentLoaded` inside an `async` script. If the script arrives late, the event may already have fired and the listener never runs.

## Related reading

- [MDN: The script element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
