Get the Current URL in JavaScript

Get the current URL your working in with JavaScript.

John Philip

This article we are going to learn how to grab the current URL your working in javascript

We are going to use the location property in the window object () which references the global window object

First, let us assign it to a variable.

Assign the const to the window.location

After the declaration, we can now “console.log” the variable to see what we get back.

In the console, something similar to these will be returned back.

In the above-logged window, we can the various properties returned including the host, hostname, href, origin, pathname and the port.

In this case, we only want the current URL we are viewing and it is referenced with the “href ” property.

We can re- assign it using another variable and log out to the console to see the output

Now lets “console.log” the variable to see out the current URL we are currently in.

look the current URL we are in

Look at what we get! amazing right

You can build awesome things with this such as you can append it to buttons are many more.

Web developer, Life Café , Curious Soul, twitter at @amjohnphilip, Email: