The Nuance of “Domain”

I love visualizations like this:

An anatomical breakdown of the parts of a URL, from scheme to domain, port, path, query string, and fragment ID.

That was my mental model of URLs until I indexed my blog’s links and realized there’s more to “domain” than initially meets the eye.

What is a “Domain”?

Let’s say you have a URL like this:

Put it in the URL constructor and you’ll get its component pieces:

    "origin": ""
    "protocol": "https:"
    "hostname": ""
    // more...

Where’s “domain”? It’s not in there anywhere…

But wait, hostname is probably what we’re thinking of, right?

But if your URL is:

The hostname will be:

Similarly, if your URL is:

Your hostname will be:

So the question is: if you have a ton of links and you want to group them by “domain” — like I did — how do you pull the “domain” out of each URL?

For example, for an unknown set of links like this:

You would want to count them by “domain” and get a result like this:

  "": 3,
  "": 2

Get the Domain Programatically

You might think, “get the hostname from the URL, split the string by periods (.), and join the last two items in the array”.

new URL("")
  .hostname   // ""
  .split(".") // ["www", "example", "com"]
  .slice(-2)  // ["example", "com"]
  .join(".")  // ""

That seems sensical, right?

The problem is, the word “domain” starts to get fuzzy and its anatomy has no universal rule for consistent parsing (such as “everything after the last period is the top-level domain”).

For example, here’s a valid domain that breaks our logic:

In that case, we don’t want as the “domain”. We want

Turns out, “domain” is a loaded term so let’s talk about words.

Top-level Domains, Domain Names, Subdomains, Root Domains, Domains, Domains, Domains!

Given a traditional URL like this:

It’s easy to think this dissects to:

But giving these things names based on position gets confusing because it’s never 100% uniform.

Again, this URL breaks expectations of naming based on positioning:

This is (I believe) why you’ll sometimes see the parts of the domain referenced simply with numbers, e.g. top-level, second-level, third-level, fourth-level, and so on. (Image courtesy of

So, as I’ve discovered, if you want to grab the “domain” (as I’ve colloquially thought of it) across a set of links, what you probably want is the “effective top-level domain” or “public suffix” which is available at

Since there is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered. This is the aim of the effective TLD list.

Need access the list programmatically from JavaScript? There’s an NPM Package For That™️: psl.

import psl from 'psl';

const parsed = psl.parse('');
console.log(parsed.domain); // ''

Per our example from earlier, now we can properly extract the “domain” from a URL:

import psl from 'psl';

const { hostname } = new URL("")
const parsed = psl.parse(hostname);
console.log(parsed.domain); // ''


Back to that graphic I started this post with:

An anatomical breakdown of the parts of a URL, from scheme to domain, port, path, query string, and fragment ID.

This graphic could lead you to believe the “domain” is a simple, straightforward part of the URL. But, as with many things in computers, the term is used loosely and its definition comes under scrutiny when dealing with the nitty gritty details of parsing URLs.

This is the nuance of the word “domain”.