Learn JavaScript
❯ getting_started.loading

Loading JS: script, defer & Modules in JavaScript

$concept

Browsers load scripts three ways: a plain <script> blocks parsing and runs immediately (rendering waits), defer postpones execution until parsing finishes in order (the default choice for classic scripts), and async runs whenever the download lands (order-free, for independent widgets). Placement and attributes decide whether users stare at blank pages.

Modern code ships as ES modules (<script type="module">): deferred by default, strict mode automatically, with real import/export between files. Node runs files directly (node app.js) with the same module system. Old patterns (scripts at body end, giant concatenated bundles) were workarounds these mechanisms replaced.

$how it works
1

Plain script blocks

<script src="app.js"> pauses HTML parsing until download plus execution finish β€” fine in head only for tiny critical code.

2

defer waits politely

defer downloads in parallel but executes after parsing, in document order β€” the right default for dependent classic scripts.

3

async runs whenever

async executes the moment it arrives, order unpredictable β€” only for independent scripts like analytics.

4

Modules are modern

<script type="module"> defers automatically, enforces strict mode, and enables import/export β€” write all new browser code this way.

$example
<!-- index.html -->
<script src="app.js" defer></script>
<script type="module" src="main.js"></script>
Output
(page parses fully first; scripts run after, in order per mechanism)
1The deferred classic script downloads alongside parsing but waits to execute.
2The module script also defers (automatic) and runs with its own scope plus import support.
3Users see content before either runs β€” no parser-blocking blank page.
$use cases
Choosing defer for every render-blocking classic script
Reserving async for order-independent third-party snippets
Writing new browser code as ES modules from day one
Running the same module code under Node without changes

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.