JavaScript WorkerLocation port Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

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

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.

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.
  • Accessself.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).

📝 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);

⚡ Quick Reference

GoalCode / note
Read portself.location.port
Return typeString
MDN example shape"8080"
Default http/httpsOften "" (empty)
Send to mainself.postMessage(location.port)
MDN statusBaseline Widely available (since July 2015)

🔍 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

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);
Try It Yourself

How It Works

The try-it lab posts the live blob worker’s port plus the MDN sample string for comparison.

Example 2 — port vs host vs hostname

Port is only the number; host may combine hostname and port.

JavaScript
self.postMessage({
  port: location.port,
  host: location.host,
  hostname: location.hostname
});
Try It Yourself

How It Works

Use port when you only need the number; use host when you want hostname + port together.

📈 Blob & Empty Ports

See how blob workers and default ports behave.

Example 3 — Blob Worker Port

Log port together with protocol and href for blob workers.

JavaScript
self.postMessage({
  port: location.port,
  protocol: location.protocol,
  href: location.href
});
Try It Yourself

How It Works

Blob workers often report an empty port—not like MDN’s "8080" sample. Always inspect the live value.

Example 4 — Is the Port Empty?

Check whether port is an empty string (common for defaults / blobs).

JavaScript
self.postMessage({
  port: location.port,
  isEmpty: location.port === ""
});
Try It Yourself

How It Works

Treat empty port as “default / not shown,” not as an error. A dedicated worker on :8080 would usually show a non-empty string.

Example 5 — Worker Port vs Page Port

Post the worker port; compare it with window.location.port.

JavaScript
// Worker
self.postMessage(location.port);

// Main
worker.onmessage = (e) => {
  console.log("worker:", e.data);
  console.log("page:", location.port);
};
Try It Yourself

How It Works

The page port often matches your local server (e.g. 3002); the blob worker port may still be empty.

🚀 Common Use Cases

  • Log the worker port for debugging panels.
  • Compare worker port with the page port.
  • Detect empty / default ports vs explicit ports like 8080.
  • Pair with host and hostname when diagnosing URLs.
  • Teach URL port concepts using workers.

🔧 How It Works

1

Worker URL

Main creates a Worker with an http(s), blob, or other script URL.

URL
2

WorkerLocation

Inside the worker, self.location describes that absolute script URL.

API
3

Read port

location.port returns the port string (or "" for defaults).

Read
4

Use or post

Compare, log, or postMessage the port to the main thread.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Only available in Web Workers (WorkerLocation / self.location).
  • Read-only string; default ports often appear as "".
  • Blob worker ports are often empty.
  • Related learning: pathname, host, hostname, Worker().

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Limited / legacy workers path (prefer modern browsers)
Legacy
WorkerLocation.port Excellent

Bottom line: Read location.port inside the worker—empty often means default port. Pair with host and hostname when debugging.

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.

Continue with protocol, pathname, host, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read port only inside a worker
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.port

The worker script’s port, as a string.

5
Core concepts
⚙️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.

Next: WorkerLocation.protocol

Learn how to read the worker's protocol string.

protocol property →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful