CHAPTER 20
Beginner
HTML Web Workers Basics
Updated: May 10, 2026
5 min read
# HTML Web Workers Basics
1. Introduction
JavaScript is "single-threaded". This means it can only do one thing at a time. If you write a script that does heavy mathematical calculations, the entire webpage will freeze until it finishes! HTML Web Workers solve this by running JavaScript files in the background, completely separate from the main webpage.2. Learning Objectives
- Understand why Web Workers are necessary.
- Create a Web Worker.
- Send and receive messages between the main page and the worker.
3. Detailed Explanations
The Problem
Imagine a script that counts to 1 billion. If you run this normally, the user can't click buttons or scroll until it finishes. The UI is blocked.The Solution: Web Workers
A Web Worker runs in the background. It doesn't have access to the DOM (it can't change HTML elements), but it can do heavy math and then send the final answer back to the main page.Step 1: Create the Worker File
Create a separate JavaScript file namedworker.js.
worker.js
Step 2: Connect the Main Page
In your HTML file, you start the worker and listen for its messages.
html