How can I get the current URL in JavaScript?
As mentioned in the Window.location Cheat Sheet, JavaScript provides a number of properties and methods to work with the current URL. Among those, Window.location.href
provides the easiest way to get the current URL as a string.
const currentURL = () => window.location.href; currentURL(); // 'https://www.google.com/'