What Are Web Workers?
You'll learn what Web Workers are and how they let JavaScript run heavy tasks without freezing your web page.
The Core Idea
JavaScript normally runs on a single thread—the main thread—which also handles UI updates, user interactions, and page rendering. If you run a heavy computation (like processing a huge dataset or performing complex calculations), the entire page freezes until that work finishes. Users can't click buttons, scroll, or see updates.
Web Workers solve this by running JavaScript in separate background threads. Think of them like hiring an assistant: your main worker (the UI thread) stays responsive and handles customer interactions, while the assistant (the Web Worker) crunches numbers in the back office. They work in parallel.
Why This Matters
When you offload intensive tasks to a Web Worker, the main thread remains free to:
- Update the UI smoothly
- Respond to user clicks and input
- Keep animations running
- Prevent the dreaded "page is unresponsive" warning
The Worker runs completely separately, communicates with the main thread via messages, and can't directly access the DOM (because touching the UI from multiple threads would cause chaos).
The Mental Model
- Main thread: handles UI, events, rendering—must stay fast and responsive
- Web Worker thread: runs heavy JavaScript tasks in the background
- Communication: they send messages back and forth (like two programs talking via a messenger)
Key Takeaway: Web Workers let you run JavaScript in background threads, keeping the main thread free so your UI never freezes during heavy computations.