Web Fundamentals

Script Loading: async vs defer

Web Fundamentals Map

Rendering & Browser Architecture

Critical Rendering PathScript Loading Patterns (async/defer)Event Loop Deep DiveJavaScript Module Systems (CJS, ESM, UMD)Dynamic Module Loading (import())Import on InteractionImport on VisibilityBrowser Rendering Pipeline & Layout ThrashingRendering Strategies (CSR, SSR, SSG, ISR)Streaming SSR & Progressive HTMLIslands ArchitectureReact Server ComponentsFramework Reactivity (React, Vue, Svelte, Solid)HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)DNS Resolution & TTL Caching

Performance

Core Web Vitals: LCP, INP, CLSPerformance Optimization Trade-offsCritical Resource PrioritizationCode Splitting & Dynamic ImportsTree Shaking & Dead Code EliminationLazy LoadingResource Hints: Preload, Prefetch & PreconnectText Compression: Gzip & BrotliImage & Video OptimizationAdaptive LoadingList VirtualizationWeb Workers vs Main ThreadMemory Leaks: Detection & PreventionManaging Third-Party ScriptsHow CDNs WorkHTTP Caching Deep DiveService Workers & Offline StrategyPWA Fundamentals

Security

Cross-Site Scripting (XSS)Cross-Site Request Forgery (CSRF)CORS ExplainedCORS Preflight, Credentials & MisconfigurationsContent Security Policy (CSP)Why is HTTPS Secure? (TLS/SSL)Authorization Best PracticesCookie Security & Session Hardening

State & Data Architecture

State Management Guide (Context vs Zustand vs Redux)React Query & Server State CachingData Fetching PatternsCaching StrategiesPagination: Offset vs Cursor-BasedReal-time Communication (WebSocket, SSE, Polling)
easyWeb Fundamentals

Script Loading: async vs defer

Learn the interview-ready mental model, practical trade-offs, and production patterns for this web fundamentals topic.

Topic content

TL;DRRegular script blocks parsing • async = parallel + immediate execution (unordered) • defer = parallel + after parsing (ordered)
Very High Signal
Google
Meta
Atlassian
Netflix
30-Second Answerstart every interview with this

Script loading attributes control parser blocking, download behavior, and execution timing. A plain <script> blocks HTML parsing. async downloads in parallel and executes as soon as ready (no order guarantee). defer downloads in parallel and executes after HTML parsing completes, in document order. type=module scripts are deferred by default.

The HTML parser is the main builder. A regular script stops the entire crew until it finishes. async workers arrive independently and start working the moment they show up. defer workers arrive early but wait politely until the main structure (HTML) is complete before starting, in the order they were hired.

Parser encounters <script>
Regular: Block & Execute
async: Download parallel → Execute ASAP
defer: Download parallel → Execute after parse (ordered)
type=module: Behaves like defer by default

1Regular Script (No Attribute)

Blocks HTML parsing completely. The browser fetches and executes the script immediately before continuing to parse the rest of the document. Use only when you truly need blocking behavior.

index.htmlhtml
<script src="app.js"></script>   <!-- parser-blocking -->

2The async Attribute

Downloads in parallel while parsing continues. Executes as soon as the script is ready — order is not guaranteed. Can still interrupt parsing when it runs. Best for independent scripts like analytics or ads.

index.htmlhtml
<script async src="analytics.js"></script>

3The defer Attribute

Downloads in parallel. Execution is delayed until HTML parsing is complete. Multiple defer scripts execute in document order. Ideal default for most application code that needs the DOM or predictable ordering.

index.htmlhtml
<script defer src="vendor.js"></script>
<script defer src="app.js"></script>

4Module Scripts (type=module)

Modern ES modules are deferred by default. They download in parallel and execute after parsing. Adding async makes them execute as soon as ready (like classic async). defer attribute has no additional effect.

index.htmlhtml
<script type="module" src="app.js"></script>
<!-- async module -->
<script type="module" async src="utils.js"></script>
Key Takeaways
  • ✓defer is the safest default for most application JavaScript
  • ✓async is ideal for independent scripts (analytics, ads)
  • ✓Regular scripts should be used sparingly — only when blocking is required
  • ✓type=module scripts are deferred by default
  • ✓Choose based on: Do you need order? Do you need the DOM? Is the script independent?
PreviousNext

Topic Guide

On this page