BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSllms.txtGitHubX
Posts/javascript
AI-generated illustration. Same delivery, two rules: serve it the moment it lands, or wait until the table is set.
Beginner4 min read

async vs defer: Why Script Order Breaks Your Page

async vs defer on a script tag: both download without blocking, but async runs the moment it arrives and defer waits for the HTML, in order. Learn which to pick.

Mohsen Fallahnejad

Mohsen Fallahnejad

Nov 27, 2026Discuss

You add async to two script tags to speed up the page. Now it breaks one load in ten with $ is not defined. The plugin ran before the library it needs.

async vs defer is a question about when a script runs, not how it downloads.

The mental model#

Both download in the background. async runs the moment it lands. defer waits in line until the HTML is done.

Picture food deliveries arriving while you set a table. async serves each dish the second it comes through the door, in whatever order the drivers arrive. defer puts every dish on a shelf and serves them in menu order once the table is fully set.

How it works#

The browser reads your HTML top to bottom and builds the page as it goes. That's called parsing. A script tag can interrupt it in three ways:

TagDownloadRunsOrder kept?
<script>Stops parsing to downloadRight awayYes
<script async>In the backgroundAs soon as it arrives, pausing parsingNo
<script defer>In the backgroundAfter the HTML is parsed, before DOMContentLoadedYes

A plain <script> is the worst case: the page stops building until the file is downloaded and run. That's why old advice said "put scripts at the bottom of <body>".

async fixes the download, but it still runs whenever it arrives. That can be mid-page, and two async scripts run in whichever order the network delivers them.

defer fixes both. It never pauses the page, and deferred scripts run in the order they appear in the HTML.

Three ribbons: one cut by a block, one dented by a block dropped mid-way, one unbroken with gold blocks waiting at its endThree ribbons: one cut by a block, one dented by a block dropped mid-way, one unbroken with gold blocks waiting at its end Plain, async, defer. Only the last one lets the page finish in one piece and still keeps the scripts in line.

In practice#

Use defer for your own app code, and for anything that depends on another script or on the DOM. It's the safe default.

Use async for scripts that stand alone and don't care about order: analytics, ads, a chat widget. If it would be fine for it to run first or last, it can be async.

html
<script defer src="/lib.js"></script>
<script defer src="/plugin.js"></script> <!-- always after lib.js -->
<script async src="https://analytics.example.com/a.js"></script>

Two quick facts help here. Module scripts (type="module") are deferred by default. And both attributes only work on scripts with a src; on an inline script, they do nothing.

How often will you hit this? Frameworks like Next.js already load their own bundles the right way. You meet it when you add a third-party snippet by hand, and that's exactly where the "works on my machine" order bug comes from.

Say it in an interview: "Both async and defer download the script without blocking the HTML parser. async runs as soon as it's downloaded, in any order, so it's for independent scripts like analytics. defer waits until the HTML is parsed and runs scripts in document order, so it's the default for app code that depends on the DOM or on other scripts."

Cheat sheet#

  • Plain script: stops the page, downloads, runs. Avoid in <head>.
  • async: serve the moment it lands. Any order. For independent scripts.
  • defer: wait until the table is set, then serve in menu order. For app code.
  • type="module": deferred by default.
  • Inline scripts ignore both attributes.

TL;DR: if order matters, use defer. If it doesn't, async is fine.

Advanced · optional

Going deeper#

Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

Both attributes at once#

<script async defer> acts as async in every modern browser. It was a trick for very old browsers that only knew defer. You don't need it today.

async on a module#

<script type="module" async> drops the wait-for-HTML rule: it runs as soon as it and its imports are ready, like a normal async script.

Scripts added from JavaScript#

A script created with document.createElement("script") acts as async by default. Set script.async = false to make dynamically added scripts run in insertion order.

Further reading#

The HTML spec diagram for script loading shows all the cases on one timeline.

Filed under#javascript#html#performance#browser#interview

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderIdempotency: Why a Retried Payment Can't Charge TwiceNewer →Sessions vs JWT: Where Does Your Login Actually Live?

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

Boolean(x) vs !!x, Number vs parseInt, and String() vs toString()

Boolean and !! match. Number and + match, except a BigInt. String(null) is "null"; null.toString() throws.

Nov 17, 20268 min read

Layout Thrashing: Why Reading offsetWidth in a Loop Freezes the Page

What layout thrashing and forced reflow are, why reading offsetWidth after a style change is slow, and how batching reads and writes fixes it.

Nov 6, 20265 min read

Debounce vs Throttle — One Sentence to Remember Forever

Two patterns for taming fast events: debounce waits for silence, throttle limits how often. With examples, code, and interview answers.

Jul 8, 20264 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

4 min left
Discuss👍 React
👍