📍 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 🗣:
locationobject browser ke current page ke “address (URL)” ke saare parts batata hai — aur tum chaho to use badal bhi sakte ho!
🧩 Common Properties
| Property | Description | Example |
|---|---|---|
| href | Returns the entire URL | location.href |
| protocol | Returns the protocol used (http: or https:) | location.protocol |
| host | Returns the hostname and port | location.host |
| hostname | Returns only the domain name | location.hostname |
| port | Returns the port number (if specified) | location.port |
| pathname | Returns the path after the domain name | location.pathname |
| search | Returns the query string part (starting with ?) | location.search |
| hash | Returns the anchor part (starting with #) | location.hash |
⚙️ Common Methods
| Method | Description | Example |
|---|---|---|
| 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 page | location.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();