Skip to the content
Start free

Async and defer scripts

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? #

A browser builds a page by reading the HTML from top to bottom and turning it into the DOM. 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 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

Speed is part of how people judge a page, and Google measures it through Core Web Vitals. A blocking script in the head delays the first paint, which can push back Largest Contentful Paint. A heavy script that runs while a visitor taps a button can hurt Interaction to Next Paint, the measure that replaced First Input Delay. Google’s Lighthouse guide to render-blocking resources flags any script that sits in the head without defer or async. That is the warning you see in PageSpeed Insights and Lighthouse.

Crawlers care too. Google’s JavaScript SEO basics explain that 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? #

Many AI crawlers fetch the raw HTML and do not run JavaScript at all. Google’s own guide says that not all bots can run it, which is why it still calls 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 entry covers this in more depth.

How does it work? #

The HTML Living Standard 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? #

  • 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 counters or a 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 #

  • 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 and CSS.
  • Deferring a cookie banner script so late that tracking tags fire before consent is read.

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 #

  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 Insights and read the render-blocking list. Soon our Core Web Vitals Checker will explain each blocking file in plain words, with the Page Speed Checker (also coming soon) beside it.
  3. Check what a bot sees without scripts. Our AI Readiness Checker reads the raw HTML; the JavaScript SEO Checker will compare raw and rendered text.
  4. Check caching and compression of your script files with the HTTP Header Checker.
  5. Shrink files with our JavaScript Minifier; see minification.
  6. Work out pass or fail from your own numbers with the Core Web Vitals Calculator.
  7. In URSY Pro: Real user speed, the Asset diet report and the JavaScript rendering check.

Questions #

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? #

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? #

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? #

It can, if one script relies on another. Test on a staging site first.

Sources #

Updated 24 November 2026 by URSY.

Cite this page: Async and defer scripts. URSY. Updated 24 November 2026. https://ursy.com/wiki/async-and-defer-scripts/ · Section links

Tools for this

  • LiveVerify an AI Botis it really GPTBot? IP, published lists and user agentRun the check →
  • Livellms.txt Checker + Generatorcheck your llms.txt, or write llms.txt, llms-full.txt and llms.jsonRun the check →
  • LiveAI Training Opt-Out Checker + Generatorwhat each AI may do with your site, and the exact lines to change itRun the check →
  • LiveAI Consistency Checkerpage, schema, llms.txt and feeds state the same facts?Run the check →
  • LiveGoogle Snippet Previewtitle and description length in pixels (in browser)Run the check →
  • LiveImage SEO Checkeralt text, AVIF/WebP, sizes and licence markupRun the check →
  • ProAsset diet reportScripts and styles each page type loads but never usesIn URSY Pro →
  • ProAI competitor gapPrompts where rivals appear and you don'tIn URSY Pro →
  • ProAI fact accuracy monitorAI quoting your prices and facts wrong, with alertsIn URSY Pro →

All 871 wiki entries, A to Z →