Loading JS: script, defer & Modules in JavaScript
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.
Plain script blocks
<script src="app.js"> pauses HTML parsing until download plus execution finish β fine in head only for tiny critical code.
defer waits politely
defer downloads in parallel but executes after parsing, in document order β the right default for dependent classic scripts.
async runs whenever
async executes the moment it arrives, order unpredictable β only for independent scripts like analytics.
Modules are modern
<script type="module"> defers automatically, enforces strict mode, and enables import/export β write all new browser code this way.
<!-- index.html --><script src="app.js" defer></script><script type="module" src="main.js"></script>Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.