Back to 30 Seconds Of Code

How can I get the current URL in JavaScript?

content/snippets/js/s/current-url.md

14.0.0430 B
Original Source

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.

js
const currentURL = () => window.location.href;

currentURL(); // 'https://www.google.com/'