How to Parse URL in JavaScript into Host, Path, Search, and Hash

Dec 3, 2021

To parse a URL in JavaScript, use the new URL() constructor. This will create a new URL object with hash, host, pathname, search, and hash properties. For the hash and search properties, it will default to an empty string if they do not exist on the URL.

const url = new URL('')
url.href; //; //
url.pathname; // "/tutorials/fundamentals/parse-url.html"; // "?num=123"
url.hash; // ""

More Fundamentals Tutorials