Worker Threads
A worker is a helper that runs code in the background.
The main page can stay smooth while heavy work happens elsewhere.
Think of a kitchen helper chopping veggies while you talk to guests.
Why workers help
Make a worker
Send messages
Listen for answers
What workers cannot do
Why use a worker
Big loops can freeze the page.
A worker moves that heavy counting off the main thread.
Clicks and typing feel better.
Create a worker
Use new Worker(url) with a script file.
That file is the helper recipe.
Talk with messages
Use postMessage to send data.
Listen with onmessage.
Data is copied
Messages send copies of data.
The worker does not share the same object box by default.
Limits
Workers cannot touch the page DOM directly.
They cannot change buttons or text by themselves.
They calculate. Then they send results back.
Good for heavy math
Good for parsing big text
Not for direct button updates
Tip: Keep workers for slow background jobs. Keep simple page updates on the main script.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.