To get the URL of the page you're currently on, JavaScript offers the Location interface that is exposed by the global Window object by your browser.

And you can access the Location interface with:


And since Window is a global object in the browser, you can access it using this also:


And if you want the href of the current page, use this:

      // ""

There are also a ton of other useful properties in the location object.

Build A Hacker News Clone
Learn how to build and deploy a full-stack, production-ready application with React.js, Node.js, and MongoDB.
View Course

Here are some examples for the page you are currently on:

      location.protocol  // "https:"      // ""
      location.hostname  // ""
      location.port      // ""
      location.pathname  // "/article/get-the-url-of-the-current-page-using-javascript"
      location.origin    // ""

You can read more about both the Window and Location interfaces on the Mozilla Developer Web Docs.

As always, thanks for reading and happy coding!