The postMessage() method of Workersends a message to the worker. Data is copied with the structured clone algorithm (or ownership can be transferred for buffers). Learn one-payload messages, arrays of values, replies via onmessage, and ArrayBuffer transfer—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
undefined
03
Sends
structured clone
04
Optional
transfer list
05
Reply
worker postMessage
06
Status
Baseline widely
Fundamentals
Introduction
After you create a worker with new Worker(), you talk to it with messages. From the main thread call worker.postMessage(data). Inside the worker, listen with self.onmessage (or addEventListener("message")) and read event.data.
The worker answers with self.postMessage(...). That is a different method on the worker global scope, but the idea is the same: clone (or transfer) data across the thread boundary.
💡
Beginner tip
You send one payload per call. Need two numbers? Send [a, b] or { a, b }—not two separate arguments like a normal function.
Concept
Understanding Worker.postMessage()
An instance method that queues a message for the worker. The first argument is mandatory and becomes event.data in the worker’s message event.
message — any structured-cloneable value (required; use null / undefined if empty).
transfer — optional array of transferable objects to move (not copy).
options.transfer — same meaning in the object form of the call.
Return value — none (undefined).
Baseline Widely available on MDN (since July 2015); available in Web Workers except Service Workers.
Changing the object on main after send does not change the worker’s copy.
Example 4 — Transfer an ArrayBuffer (MDN Idea)
After transfer, the sender’s byteLength becomes 0.
JavaScript
const buf = new ArrayBuffer(8);
console.log(buf.byteLength); // 8
worker.postMessage(buf, [buf]);
console.log(buf.byteLength); // 0 — ownership moved
// worker can transfer it back the same way
Worker.postMessage() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is available in Web Workers except Service Workers.
✓ Baseline · Widely available
Worker.postMessage()
Sends a structured-clone message to a dedicated worker; optional transfer moves buffer ownership.
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 ExplorerSupported with workers in IE10+ (prefer modern browsers)
Legacy
Worker.postMessage()Excellent
Bottom line: Use postMessage to talk to workers; pack multiple values in arrays/objects, and transfer large buffers when you need zero-copy.
Wrap Up
Conclusion
Worker.postMessage() is how the main thread sends data to a dedicated worker. Clone by default, transfer when you must move large buffers, and always pass an explicit message value.
Listen with onmessage before posting if you need the reply
Transfer large ArrayBuffers intentionally
Pass null explicitly for empty pings
Keep message shapes documented in your app
❌ Don’t
Expect functions or DOM nodes to clone
Use a transferred buffer after sending it
Call postMessage() with zero arguments
Assume shared references across threads
Forget error handlers on long-lived workers
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about postMessage()
The mailbox from main to worker.
5
Core concepts
💬01
Sends
to worker
API
📦02
One payload
array / object
Shape
🔁03
Clone
structured
Copy
⚡04
Transfer
buffers move
Perf
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
It sends a message from the main thread to a dedicated Worker. The data is delivered in the message event's data field inside the worker (via structured clone).
No. MDN marks Worker.postMessage() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is available in Web Workers except Service Workers.
Almost any value the structured clone algorithm supports: strings, numbers, booleans, arrays, plain objects, Date, Map, Set, ArrayBuffer, and more—including objects with cyclical references. Functions and DOM nodes cannot be cloned.
postMessage sends one message payload. Put several values in an array or object, for example postMessage([a, b]) or postMessage({ a, b }).
An optional list of transferable objects (like ArrayBuffer) moves ownership to the worker. After transfer, the original buffer is neutered (often byteLength === 0) on the sending side.
No. It is mandatory. If you have nothing useful to send, pass null or undefined explicitly.
Did you know?
Structured clone can handle cyclical object graphs—something JSON.stringify cannot. That is why workers can receive richer data than a JSON round-trip would allow.