The Worker() constructor creates a dedicated Worker that runs a classic script or module at a URL on a background thread. Learn url rules, type / name options, blob URLs for demos, and messaging with postMessage—with five examples and try-it labs.
01
Create
new Worker()
02
URL
same-origin / blob
03
Type
classic | module
04
Name
debug label
05
Talk
postMessage
06
Status
Baseline widely
Fundamentals
Introduction
Heavy work on the main thread can freeze clicks and scrolling. A dedicated worker runs JavaScript in parallel. You start it with new Worker(url), send data with worker.postMessage(...), and listen with worker.onmessage (or addEventListener("message")).
The worker script must usually be same-origin with your page (or a blob: / data: URL). In these tutorials we often build a small script as a Blob so the demo works in one HTML file.
💡
Beginner tip
Workers cannot touch the DOM. They talk to the page with messages. Think “mailbox,” not “shared variables.”
⚠️
Security note
The URL you pass is executed. Never pass untrusted user URLs into new Worker() without strict allow-lists, CSP (worker-src), and preferably Trusted Types (TrustedScriptURL).
Concept
Understanding the Worker() Constructor
A Web API constructor that returns a Worker object running the script or module at url.
url — string or TrustedScriptURL; same-origin, or blob: / data:.
options.type — "classic" (default) or "module".
options.name — debug name for the worker scope.
options.credentials — for module workers: omit, same-origin (default), or include.
Baseline Widely available on MDN (since July 2015); some parts may vary. Available in Web Workers except Service Workers.
Foundation
📝 Syntax
JavaScript
new Worker(url)
new Worker(url, options)
Parameters
Parameter
Meaning
url
Script/module URL (TrustedScriptURL or string)
options.type
"classic" or "module" (default classic)
options.name
Identifying name (handy for debugging)
options.credentials
Module import credentials; ignored for classic
Return value
A new Worker object.
Classic worker (file)
JavaScript
const myWorker = new Worker("worker.js");
// or explicitly:
const classic = new Worker("worker.js", { type: "classic" });
Module worker
JavaScript
const moduleWorker = new Worker("worker_module.js", {
type: "module"
});
Worker() is marked Baseline Widely available on MDN (since July 2015). Some options (for example module workers) may have varying support. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Worker()
Creates a dedicated Worker that runs a classic script or module at the given URL.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerWorkers supported in IE10+ (prefer modern browsers)
Legacy
Worker()Excellent
Bottom line: Use new Worker(url) for background scripts; start with classic + blob demos, then add module workers when you need ES imports.
Wrap Up
Conclusion
new Worker(url, options) starts a dedicated background script. Keep URLs same-origin (or blob), choose classic vs module deliberately, and talk to the worker with messages—never by sharing the DOM.
Prefer blob URLs over data URLs for generated scripts
Set name when debugging multiple workers
Handle onerror / message errors
Revoke blob URLs after terminate()
❌ Don’t
Pass untrusted URLs into new Worker()
Expect DOM access inside the worker
Forget CORS when loading cross-origin module scripts
Leave unused workers running forever
Skip CSP worker-src on production sites
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Worker()
Background scripts started with a URL.
5
Core concepts
⚙️01
new Worker
starts thread
API
🔗02
URL
same-origin / blob
Input
📦03
classic|module
type option
Mode
💬04
Messages
postMessage
Talk
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
new Worker(url, options) creates a dedicated Worker that runs a classic script or module at the given URL on a background thread, separate from the main page UI thread.
No. MDN marks the Worker() constructor as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some options may have varying support. It is available in Web Workers except Service Workers.
A same-origin script URL (resolved relative to the HTML page), or a blob: or data: URL. Cross-origin worker scripts usually need an intermediate same-origin worker or a blob loaded after a CORS fetch.
type: "classic" (default) runs a classic script and can use importScripts(). type: "module" runs an ES module with import, CORS fetching, and strict mode.
In a single HTML file you often have no separate worker.js. Building a Blob of JavaScript and passing URL.createObjectURL(blob) creates a same-origin worker URL you can start immediately.
Yes if the URL comes from untrusted input—the script is executed (an injection sink / XSS risk). Prefer fixed same-origin scripts, CSP worker-src, and TrustedScriptURL when enforcing Trusted Types.
Did you know?
Bundlers like webpack, Vite, and Parcel often recommend new Worker(new URL("worker.js", import.meta.url)) so the worker path stays relative to the script, not the HTML page—safer for renaming and packaging.