Published on

How to get Subdomain from URL in JavaScript


Here's a how you can get the subdomain from the URL string in Javascript 👇

const getSubdomain = url => {
    let domain = url;
    if (url.includes("://")) {
        domain = url.split('://')[1];
    const subdomain = domain.split('.')[0];
    return subdomain;

Here is an example of the above snippet

Happy finding subdomain!