defer, async, and type="module": How Scripts Actually Load

defer, async, and type="module" change when a script runs relative to HTML parsing. Use defer for ordered classic scripts, async for independent code, and a module script when the code uses JavaScript modules.

The distinction matters most when a script needs parsed markup or another file to have run first. Downloading without pausing the parser is useful, but execution timing still needs to match those dependencies.

Compare defer, async, and type=“module”

For script elements written in the initial HTML, the main cases are:

ScriptFetching and parsingExecution timingOrdering
External classic, no attributesThe parser waits at the scriptBefore parsing continuesParser order
External classic with deferFetch overlaps parsingAfter parsing, before DOMContentLoadedDocument order among deferred classic scripts
External classic with asyncFetch overlaps parsingWhen ready, potentially before parsing finishesNo document-order guarantee
Module without asyncModule graph loads alongside parsingStarts after parsingDependencies follow module evaluation rules
Module with asyncModule graph loads alongside parsingWhen the graph is readyNo document-order guarantee

The script element documentation describes these attributes. Parser-inserted scripts are the scope of this comparison; scripts created dynamically have additional timing rules.

Give dependent classic scripts defer

Two classic files can keep a dependency order without blocking parsing while they download:

<script src="/js/library.js" defer></script>
<script src="/js/app.js" defer></script>

The browser executes library.js before app.js, even if the second file finishes downloading first. Both run after the HTML has been parsed, so the second file can also query elements from the document.

That ordering applies to these deferred classic scripts. It does not organise unrelated asynchronous scripts or promise that every other resource has finished loading.

A plain external classic script blocks parsing while it loads and runs:

<script src="/js/app.js"></script>

When the parser reaches that element, it waits for the script to be ready and execute. The HTML script processing model distinguishes that blocking path from deferred and asynchronous processing.

Putting the plain script near the end of the body means more markup has already been parsed. Adding defer makes the timing explicit. Head placement also lets the browser discover that file earlier.

Reserve async for code that can run independently

An independent script can run when its file is ready:

<script src="/js/independent-widget.js" async></script>

Independence includes the DOM, not just other JavaScript files. A widget that expects a later element to exist cannot assume parsing has reached it.

Do not mark a library and its consumer async and expect their tag order to protect the dependency. The smaller file may arrive first. Either script may run before HTML parsing finishes.

An analytics script may be a candidate if its integration supports asynchronous loading. That label alone does not make every third-party snippet independent. Follow the provider’s loading instructions.

Let modules use their default timing

A module entry point normally needs only its type and source:

<script type="module" src="/js/main.js"></script>

The browser discovers the entry point’s static imports as part of loading its dependency graph. A non-async module begins evaluation after parsing, so adding defer does not supply an extra benefit.

JavaScript modules also have their own scope and run in strict mode. Converting a classic script to a module can therefore change behaviour beyond its loading time.

For example, a top-level module binding does not automatically become a property on window. Code expecting a global function from an earlier classic file may need a real import. Check that integration when changing the script’s type.

Inline scripts have different attribute rules

An ordinary inline classic script runs when the parser reaches it. Adding defer to that inline script does not postpone it; the attribute applies to external classic scripts.

An inline module follows module timing instead:

<script type="module">
  document.querySelector("#message")?.append(" Ready.");
</script>

<p id="message">Page content.</p>

The paragraph is parsed before this non-async inline module runs. That timing also applies to a small module written in the head.

Parsed HTML is not a fully ready interface

DOMContentLoaded follows parsing and the execution of deferred scripts. It does not wait for images to finish, and asynchronous work started inside a script can continue afterward.

A parsed document also says nothing about elements another application adds later. Query the markup that actually exists, and use a separate readiness signal when an interaction depends on fetched data or delayed rendering.

The module timing article looks more closely at imports, inline modules, and asynchronous exceptions.

For a small site, ordered classic code fits defer; imported code fits a module entry point. Keep async for scripts whose contract allows it. None of these choices makes a long-running script cheap once execution begins. Removing unnecessary work still matters.

Last updated on and filed under , and .