Script Loading
How the browser handles `
A bare `` fetches, parses, and executes immediately, blocking HTML parsing. With `defer`, the script waits until HTML parsing is complete. With `async`, it fetches and runs as soon as it's ready, in parallel with parsing. `type="module"` is implicitly defer. Dynamically created scripts are implicitly async unless you set an onload handler.
Modern apps split JavaScript into multiple chunks: critical startup code runs inline in `
`, the rest loads asynchronously. This "critical path" optimization is often the only difference between an 8-second and a 2-second initial load. Tools like webpack and esbuild offer code splitting and dynamic imports to make this easier.