The port property of a WorkerLocation object returns the port of the worker’s location as a string. Learn self.location.port, how it differs from host and hostname, why default ports can be empty, and what blob workers report—with five examples and try-it labs.
01
Kind
Instance property
02
Returns
String
03
Access
self.location
04
Writable?
Read-only
05
Context
Web Workers only
06
Status
Baseline widely
Fundamentals
Introduction
A port is the number that identifies which service on a host is being contacted—for example 8080 in http://localhost:8080/. Inside a worker, self.location.port returns that port for the worker script URL as a string.
MDN’s classic idea: on a worker related to http://localhost:8080/, location.port can look like "8080".
💡
Beginner tip
Default ports often return "" (empty string)—not "80" or "443". For hostname plus port when shown, use host.
Concept
Understanding the port Property
A read-only instance property on WorkerLocation that returns the worker’s port as a string.
Value — a string (e.g. "8080", "3002", or "").
vs host — port is only the number; host may include hostname + port.
Access — self.location.port or location.port inside the worker.
Workers only — not available on the main document thread.
Baseline Widely available on MDN (since July 2015).
Foundation
📝 Syntax
Read the property inside a worker script:
JavaScript
const result = self.location.port;
// e.g. "8080"
Value
A string.
Typical pattern (MDN idea)
JavaScript
// Inside a Web Worker
const result = self.location.port;
self.postMessage(result);
Compare
⚖️ port vs Related Ideas
Idea
Meaning
location.port
Worker port string (or "")
location.host
Hostname + port when shown
location.hostname
Hostname without the port
location.origin
Scheme + host + port in origin form
location.href
Full serialized worker URL
window.location.port
Port of the page (main thread)
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read port
self.location.port
Return type
String
MDN example shape
"8080"
Default http/https
Often "" (empty)
Send to main
self.postMessage(location.port)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about WorkerLocation.port.
Returns
port
String
Where
in worker
self.location
Writable
no
Read-only
Baseline
widely
Since Jul 2015
Hands-On
Examples Gallery
Examples follow MDN WorkerLocation.port. Labs use blob workers and compare port with related URL parts.
📚 Getting Started
Read the port the MDN way.
Example 1 — Read self.location.port (MDN Idea)
Post the worker port string to the main thread.
JavaScript
const result = self.location.port;
// e.g. "8080"
self.postMessage(result);
WorkerLocation.port is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is only available in Web Workers.
✓ Baseline · Widely available
WorkerLocation.port
Returns the worker's port as a string. Read it with self.location.port inside a worker.
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 ExplorerLimited / legacy workers path (prefer modern browsers)
Legacy
WorkerLocation.portExcellent
Bottom line: Read location.port inside the worker—empty often means default port. Pair with host and hostname when debugging.
Wrap Up
Conclusion
WorkerLocation.port is the worker-side port string for the script URL. Read it inside the worker, expect empty values for defaults and many blob demos, and use host when you need hostname + port together.
Compare with window.location.port on the main thread
Treat empty port as default / not shown
Log host and href alongside port
Use postMessage to surface the value on the page
❌ Don’t
Expect WorkerLocation on the main thread
Assign to location.port in a worker (read-only)
Assume default https always shows "443"
Assume blob demos always match MDN’s 8080 example
Confuse port with the full host string
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about WorkerLocation.port
The worker script’s port, as a string.
5
Core concepts
🔗01
Returns
port string
API
⚙️02
Workers
self.location
Context
🔒03
Read-only
no assign
Rule
📄04
Empty OK
defaults / blobs
Gotcha
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
A string: the port part of the worker's location. Access it inside a worker as self.location.port or location.port.
No. MDN marks WorkerLocation.port as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
host is hostname plus port when the port is shown. port is only the port number as a string (or an empty string when the default port is used).
For default ports (such as 80 for http or 443 for https), location.port is often the empty string "". Non-default ports like 8080 or 3002 usually appear as "8080" or "3002".
Blob workers often report an empty port. Always log port together with host, hostname, and href when debugging.
No. WorkerLocation.port is read-only.
Did you know?
MDN’s example uses a non-default port 8080 so the property is easy to see. On plain https:// sites, location.port is often "" even though the connection uses port 443.