HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

How can I get the current URL in JavaScript?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
urljavascripthowgetthecancurrent

Problem

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.

Solution

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

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

Code Snippets

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

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

Context

From 30-seconds-of-code: current-url

Revisions (0)

No revisions yet.