Get the Url of the Current Page Using JavaScript

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:
JavaScript
 
window.location
And since Window is a global object in the browser, you can access it using this also:
JavaScript
 
location
And if you want the href of the current page, use this:
JavaScript
 
location.href
// "https://coderrocketfuel.com/article/get-the-url-of-the-current-page-using-javascript"
There are also a ton of other useful properties in the location object.
Here are some examples for the page you are currently on:
JavaScript
 
location.protocol  // "https:"
location.host      // "coderrocketfuel.com"
location.hostname  // "coderrocketfuel.com"
location.port      // ""
location.pathname  // "/article/get-the-url-of-the-current-page-using-javascript"
location.origin    // "https://coderrocketfuel.com"
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!