No Time to Read - Copy/Paste Solution

function getCurrentURL () {
return window.location.href
}
// Example
const url = getCurrentURL()
url // https://ui.dev/get-current-url-javascript/

Simple enough. If you're using JavaScript in the browser you can get the full current URL by using window.location.href. I think it's also worth noting that window.location has a bunch of different properties on it that may be of use to you as well.

Let's assume this is the current URL we're on

https://ui.dev/get-current-url-javascript/?comments=false

These are all the properties that window.location gives us.

const {
host, hostname, href, origin, pathname, port, protocol, search
} = window.location
host // "ui.dev"
hostname // "ui"
href // "https://ui.dev/get-current-url-javascript/?comments=false"
origin // "https://ui.dev"
pathname // "/get-current-url-javascript/""
port // ""
protocol // "https:"
search // "?comments=false"

Before you leave

I know, "another newsletter pitch" - but hear me out. Most JavaScript newsletters are terrible. When's the last time you actually looked forward to getting one? Even worse, when's the last time you actually read one rather than just skim it?

We wanted to change that, which is why we created Bytes. The goal was to create a JavaScript newsletter that was both educational and entertaining. 101,890 subscribers and an almost 50% weekly open rate later, it looks like we did it.

Delivered to 101,890 developers every Monday