Let’s say you’re building some type of internal search function on your website. To do that properly you need to access all the data values of a URL string.
new URL() that allows us to convert a URL into an object that gives us a nicely organized overview of every property and value in URLs.
To use it, create a variable, and assign it a value of
new URL('your-url'). For example, below I’m creating a URL object based on a URL for one of TechStacker’s articles:
Now try logging out the result:
Your returned URL object will look similar to this:
And now you can easily access each property and do what you want with it. For example, to get access to the
pathname of your URL, you’d do this: