How to use the URL Object in JavaScript

In JavaScript, the URL interface is used to parse, construct, normalize, and encode URLs. It provides static methods and properties to read and modify different components of the URL.

Create a URL object

You can create a new URL object by either passing the string URL or by providing a relative path and a base string URL to its constructor:

// URL object with absolute path
const url1 = new URL('')

// URL object with relative path and base URL
const url2 = new URL('/blog/javascript-url-object', '')

In the example above, both URLs are the same. We can even create a new URL based on the path relative to an existing URL:

const url3 = new URL('/blog', url1)

URL Properties

The URL object is useful if you want to extract different parts from a string URL, such as the hostname, port, relative path, and parameter values. You can access these properties immediately after the URL object is created:

const url = new URL('')

console.log(url.protocol) // https:
console.log( //
console.log(url.pathname) // /blog/javascript-url-object
console.log(url.hash) // #url-properties
console.log(url.origin) //

In addition to the above properties, the URL object also has:

  • search — The query parameters string including the leading ? character.
  • href — The complete URL, same as url.toString() method.
  • port — Returns the port of the URL.
  • searchParams — A URLSearchParams object to access the individual query parameters found in search.
  • username & password — Only available if HTTP authentication is used.

Apart from the above properties, the URL object also provides two methods:

  • toString() — It is similar to url.href but cannot be used to modify the value.
  • toJSON() — It returns the same string as href property.

Update a URL object

The URL object properties (except origin and searchParams) can be used to construct a new URL or update parts of an existing URL:

// construct a URL
const url = new URL('')
url.pathname = '/blog/javascript-url-object'
url.hash = '#url-properties'

// update `protocol` property
url.protocol = 'https:'


Static Methods

The URL interface provides a createObjectURL() static method to generate a blob URL (starts with blob: as its schema) that uniquely identifies the object in the browser:

const blobUrl = URL.createObjectURL(blob)

Once you have the blob URL, pass it to the revokeObjectURL() static method to remove it from memory:


URL Object Usage

At the moment, the URL object usage is limited. Simple strings are good enough for making network requests. However, you can use the URL object in modern JavaScript APIs like Fetch API or even in XMLHttpRequest (XHR) to communicate with the server.

Here is an example of the Fetch API that uses a URL object to get a JSON object:

const url = new URL('')

  .then(res => res.json())
  .then(res => { => {
      console.log(`${}: ${user.first_name} ${user.last_name}`)

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.