# Script Loading Strategies

> How to use async and defer attributes to optimize script loading and prevent render-blocking.

Subject: Internet | Lesson 32 of 35 | 6 min read
Interactive lesson: https://codewithanimation.in/internet/script-loading-strategies
Previous: [Render-Blocking Resources](https://codewithanimation.in/internet/render-blocking-resources.md) | Next: [Layout Thrashing](https://codewithanimation.in/internet/layout-thrashing.md)

## What it is

Script Loading Strategies determine how the browser downloads and executes JavaScript files in relation to HTML parsing. There are three main approaches:

1. **Synchronous (Default)**: The browser stops HTML parsing, downloads the script, executes it, and then resumes parsing.

2. **Async**: The browser downloads the script in the background while parsing HTML. As soon as the download finishes, it pauses parsing to execute the script.

3. **Defer**: The browser downloads the script in the background while parsing HTML. It waits until HTML parsing is 100% complete before executing the script.

## Why it matters

Optimizing script loading is crucial for a fast First Contentful Paint (FCP).

Synchronous scripts cause the 'white screen of death' by blocking the main thread.

Understanding `async` and `defer` allows you to download scripts early without halting the rendering process, while ensuring that dependent scripts execute in the correct order.

## How it works (The Timeline)

**Sync:** `Parse HTML` 🛑 → `Download` → `Execute` → `Resume Parse HTML`

**Async:** `Parse HTML` (while `Download` in parallel) 🛑 → `Execute` (as soon as downloaded) → `Resume Parse HTML`

**Defer:** `Parse HTML` (while `Download` in parallel) ✅ → `Finish Parse HTML` → `Execute` (in exact document order)

## Common mistake: The 'Async Everything' Mistake

Beginners often use `async` everywhere thinking 'it makes things faster'.

Because `async` scripts execute exactly when they finish downloading, a smaller dependent script might finish before the large library it relies on (e.g., executing app code before React has loaded).

This causes intermittent race conditions and 'undefined variable' errors that are notoriously hard to reproduce.

## Tip: Pro tip

**Defer everything you can. Async only for isolated things.**

Use `defer` for your application code, UI libraries, and anything that manipulates the DOM. It gives you background downloading *plus* guaranteed execution order.

Save `async` strictly for independent, third-party scripts that don't interact with your code, like Google Analytics or ad pixels.

## Try it: Try it

Watch the Construction Site Race Track. See how a bulldozer (HTML Parser) reacts to delivery trucks (Scripts) depending on the loading strategy.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/script-loading-strategies

## Check yourself

### Question 1

What happens to HTML parsing when a standard (synchronous) `<script>` tag is encountered?

- A. It continues parsing in parallel while the script downloads.
- B. It stops immediately to download and execute the script.
- C. It finishes parsing the whole document before downloading the script.
- D. It stops parsing but downloads the script in the background.

Correct answer: B

By default, scripts are parser-blocking. The browser must stop parsing the HTML, download the script, and execute it before continuing, which delays page rendering.

- Why not A: This describes async or defer behavior.
- Why not C: Standard scripts block immediately upon discovery.
- Why not D: Standard scripts block both downloading of subsequent assets and HTML parsing.

### Question 2

You have three scripts that depend on each other (A before B before C). Which attribute should you use to download them in the background but maintain execution order?

- A. async
- B. defer
- C. sync
- D. preload

Correct answer: B

The 'defer' attribute downloads scripts in parallel with parsing but guarantees they will execute in the exact order they appear in the HTML document, only after parsing is complete.

- Why not A: async executes scripts in the order they finish downloading, causing race conditions here.
- Why not C: sync isn't a valid attribute; default scripts block parsing entirely.
- Why not D: preload is a resource hint, not an execution strategy.

### Question 3

When is the best time to use the `async` attribute for a script?

- A. For your main application bundle.
- B. For a script that relies on a large library loaded earlier.
- C. For independent, third-party scripts like analytics or ad trackers.
- D. For scripts that modify the visual structure of the page.

Correct answer: C

async is ideal for completely independent scripts (like Google Analytics) that don't care when they run, don't depend on other scripts, and don't affect the initial rendering of the page.

- Why not A: async might execute before the DOM is ready, causing elements to not be found.
- Why not B: Execution order isn't guaranteed, so the library might not be ready.
- Why not D: Modifying the DOM mid-load with an async script can cause jarring layout shifts.

## Remember this

- Default scripts block parsing AND downloading.
- Async scripts download in parallel but block parsing to execute immediately.
- Defer scripts download in parallel and wait for parsing to finish before executing.
- Defer guarantees execution order. Async does not.
