---
title: "Async and defer scripts"
description: "Async and defer let the browser download JavaScript while it reads the page. Async runs on arrival; defer runs after parsing, in order."
url: https://ursy.com/wiki/async-and-defer-scripts/
updated: 2026-11-24T23:06:43+00:00
publisher: URSY (https://ursy.com/)
cite: Name URSY and link https://ursy.com/wiki/async-and-defer-scripts/
---

# Async and defer scripts

> Async and defer let the browser download JavaScript while it reads the page. Async runs on arrival; defer runs after parsing, in order.

**Async and defer are two attributes you add to a script tag so the browser downloads the JavaScript file while it keeps reading the page, instead of stopping everything to fetch and run it.** Async runs the file the moment it arrives. Defer waits until the whole page has been parsed and keeps your scripts in order.

## What is it? {#what-is-it}

A browser builds a page by reading the [HTML](https://ursy.com/wiki/html/) from top to bottom and turning it into the [DOM](https://ursy.com/wiki/dom-document-object-model/). A plain `<script src>` tag in the head makes it stop, fetch the file, run it, and only then carry on. Nothing below that tag can be shown in the meantime. The two attributes change that pause:

- **async** : the file is fetched in parallel with parsing and run as soon as it is ready. MDN’s [script element reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script) warns there is no promise about the order several async files will run in.
- **defer** : the file is fetched in parallel and run after the document has been parsed, just before the DOMContentLoaded event. Deferred files run in the order they appear in the page.

```
<script src="app.js"></script>          stops the parser
<script async src="stats.js"></script>  runs when it lands
<script defer src="menu.js"></script>  runs after parsing, in order
```

## Why does it matter for search? {#why-does-it-matter-for-search}

Speed is part of how people judge a page, and [Google](https://ursy.com/wiki/google/) measures it through [Core Web Vitals](https://ursy.com/wiki/core-web-vitals/). A blocking script in the head delays the first paint, which can push back [Largest Contentful Paint](https://ursy.com/wiki/largest-contentful-paint/). A heavy script that runs while a visitor taps a button can hurt [Interaction to Next Paint](https://ursy.com/wiki/interaction-to-next-paint/), the measure that replaced [First Input Delay](https://ursy.com/wiki/first-input-delay/). Google’s [Lighthouse guide to render-blocking resources](https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources) flags any script that sits in the head without defer or async. That is the warning you see in [PageSpeed Insights](https://ursy.com/wiki/google-pagespeed-insights/) and [Lighthouse](https://ursy.com/wiki/google-lighthouse/).

Crawlers care too. Google’s [JavaScript SEO basics](https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics) explain that [Googlebot](https://ursy.com/wiki/googlebot/) crawls a page, queues it, then renders it in a headless Chromium before it indexes the result. Async and defer do not hide content from that render. They just stop your scripts from holding up the visible page.

## Why does it matter for AI answers? {#why-does-it-matter-for-ai-answers}

Many [AI crawlers](https://ursy.com/wiki/ai-crawler/) fetch the raw HTML and do not run JavaScript at all. Google’s own guide says that not all [bots](https://ursy.com/wiki/bot-web-robot/) can run it, which is why it still calls [server-side rendering](https://ursy.com/wiki/server-side-rendering/) or pre-rendering a great idea. Async and defer do not fix that. They only change when a script runs in a browser. If your prices, headings or answers only appear after a script runs, an AI system may never see them. Put the words in the HTML, and use scripts for extras. Our [JavaScript SEO](https://ursy.com/wiki/javascript-seo/) entry covers this in more depth.

## How does it work? {#how-does-it-work}

The [HTML Living Standard](https://html.spec.whatwg.org/multipage/scripting.html) sets the rules. In short:

| Tag | Download | Runs | Order kept? |
| --- | --- | --- | --- |
| No attribute | Parser stops | At once | Yes |
| async | Alongside parsing | As soon as it arrives | No |
| defer | Alongside parsing | After parsing ends | Yes |
| type=”module” | Alongside parsing | After parsing ends | Yes |
| type=”module” async | Alongside parsing | As soon as it arrives | No |

Four details trip people up. Module scripts are deferred by default, so adding defer to them does nothing. Neither attribute has any effect on an inline script with no `src`. If both are present, the browser acts as if only async is set; the standard lets you add both so very old browsers fall back to defer. And a deferred script holds back DOMContentLoaded until it has run.

## Which one should I use? {#which-one-should-i-use}

- **defer**  for your own code that touches the page: menus, sliders, forms, anything that needs the DOM or another script first.
- **async**  for independent files that depend on nothing and nothing depends on them, such as some [analytics](https://ursy.com/wiki/web-analytics/) counters or a [Google Tag Manager](https://ursy.com/wiki/google-tag-manager/) container.
- **Inline** , small and in the head, for the few lines the first view truly needs. The Lighthouse guide suggests this for critical code.
- **Remove**  what is not used. The same guide says unused code should go. Fewer bytes beat any attribute.

## Common mistakes {#common-mistakes}

- **Async on scripts that depend on each other.**  A plugin set to async can run before the library it needs and throw an error.
- **Defer on an inline script.**  It is ignored, so the script still blocks.
- **Calling a deferred function from inline code higher up.**  The function does not exist yet when the inline code runs.
- **Hiding core text behind scripts.**  Fast for people, empty for bots that read only HTML.
- **Forgetting CSS.**  Stylesheets in the head also block the first paint; see the [critical rendering path](https://ursy.com/wiki/critical-rendering-path/) and [CSS](https://ursy.com/wiki/css/).
- **Deferring a [cookie banner](https://ursy.com/wiki/cookie-banner/) script**  so late that tracking tags fire before consent is read.

## A worked example {#a-worked-example}

A small online shop has 4 files in its head: a 90 KB library, a slider that needs it, a chat widget and an analytics tag. All 4 block. The page stays blank until every file has downloaded and run.

1. Library and slider get `defer`. They still run in order, after parsing.
2. The analytics tag gets `async`, since nothing waits for it.
3. The chat widget is moved to load after the visitor scrolls or clicks.
4. Product names, prices and stock stay in the HTML, so crawlers read them without running anything.

The head no longer holds up the first paint, and the render-blocking warning goes. Measure before and after rather than trusting a rule of thumb.

## How to check it {#how-to-check-it}

1. View the page source and look at every `<script>` in the head. Note which have src, async, defer or type=”module”.
2. Run Lighthouse or [PageSpeed](https://ursy.com/wiki/page-speed/) Insights and read the render-blocking list. Soon our [Core Web Vitals Checker](https://ursy.com/core-web-vitals-checker/) will explain each blocking file in plain words, with the [Page Speed Checker](https://ursy.com/page-speed-checker/) (also coming soon) beside it.
3. Check what a bot sees without scripts. Our [AI Readiness Checker](https://ursy.com/ai-readiness-checker/) reads the raw HTML; the [JavaScript SEO Checker](https://ursy.com/javascript-seo-checker/) will compare raw and rendered text.
4. Check [caching](https://ursy.com/wiki/caching/) and compression of your script files with the [HTTP Header Checker](https://ursy.com/http-header-checker/).
5. Shrink files with our [JavaScript Minifier](https://ursy.com/webmaster-tools/javascript-minifier/); see [minification](https://ursy.com/wiki/minification/).
6. Work out pass or fail from your own numbers with the [Core Web Vitals Calculator](https://ursy.com/webmaster-tools/core-web-vitals-calculator/).
7. In URSY Pro: [Real user speed](https://ursy.com/pro/real-user-speed/), the [Asset diet report](https://ursy.com/pro/asset-diet-report/) and the [JavaScript rendering check](https://ursy.com/pro/javascript-rendering-check/).

## Questions {#questions}

### Is defer always better than async? {#is-defer-always-better-than-async}

No. Defer is the safer default for code that needs the page or other scripts. Async suits small, independent files.

### Can I put deferred scripts in the head? {#can-i-put-deferred-scripts-in-the-head}

Yes. That is the usual place. The browser finds them early, downloads them alongside parsing and runs them at the end.

### Does Google rank pages higher for using defer? {#does-google-rank-pages-higher-for-using-defer}

Not directly. It can make the page faster, and speed is one part of page experience. Good content still matters far more.

### Will async break my site? {#will-async-break-my-site}

It can, if one script relies on another. Test on a [staging site](https://ursy.com/wiki/staging-site/) first.

## Related words {#related-words}

- [Critical rendering path](https://ursy.com/wiki/critical-rendering-path/)
- [JavaScript SEO](https://ursy.com/wiki/javascript-seo/)
- [Rendering](https://ursy.com/wiki/rendering/)
- [Lazy loading](https://ursy.com/wiki/lazy-loading/)
- [Why your website is slow](https://ursy.com/wiki/why-your-website-is-slow/)

## Sources {#sources}

- [MDN: The script element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script), checked 24 November 2026
- [WHATWG HTML Living Standard: Scripting](https://html.spec.whatwg.org/multipage/scripting.html), checked 24 November 2026
- [Chrome for Developers: Eliminate render-blocking resources](https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources), checked 24 November 2026
- [Google Search Central: Understand JavaScript SEO basics](https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics), checked 24 November 2026

*Updated 24 November 2026 by URSY.*
