📍 Location Object

Last Updated: 26th October 2025


The Location Object provides information about the current URL of the browser and allows you to redirect or reload the page.
It is a property of the window object (window.location).

Hinglish Tip 🗣: location object browser ke current page ke “address (URL)” ke saare parts batata hai — aur tum chaho to use badal bhi sakte ho!


🧩 Common Properties

PropertyDescriptionExample
hrefReturns the entire URLlocation.href
protocolReturns the protocol used (http: or https:)location.protocol
hostReturns the hostname and portlocation.host
hostnameReturns only the domain namelocation.hostname
portReturns the port number (if specified)location.port
pathnameReturns the path after the domain namelocation.pathname
searchReturns the query string part (starting with ?)location.search
hashReturns the anchor part (starting with #)location.hash

⚙️ Common Methods

MethodDescriptionExample
assign()Loads a new document (redirects)location.assign("https://google.com")
replace()Replaces current page (no history entry)location.replace("https://example.com")
reload()Reloads the current pagelocation.reload()

Example

console.log("Full URL:", location.href);
console.log("Host:", location.host);
console.log("Pathname:", location.pathname);
console.log("Protocol:", location.protocol);
console.log("Search Params:", location.search);

// Redirect example
// location.assign("https://www.google.com");

// Reload example
// location.reload();