JavaScript location.host vs location.hostname
Last Updated :
23 Jul, 2025
In this article, we will learn about how to get information related to the webpage like hostname, port number, etc. We will also see the difference between the location.hostname and location.host.
location object is used to get information about the current web page. host and hostname are properties of location objects.
location.hostname: This property will return the domain name of the web page.
Syntax:
location.hostname
Return value: It returns a string representing the domain name or IP address.
If this is our URL,
http://www.geeksforgeeks.org:8080/ds/heap
When we call location.hostname, This will return.
www.geeksforgeeks.org
Example :
JavaScript
console.log("hostname : " + location.hostname);
location.host: This property also returns the same hostname but it also includes the port number. In case, if the port number is not available, then it will just return the hostname.
Syntax:
location.host
Return value: It returns a string representing the domain name and port number.
If this is our URL,
http://www.geeksforgeeks.org:8080/ds/heap
When we call location.host, This will return.
www.geeksforgeeks.org:8080
Example :
JavaScript
console.log("host : " + location.host);
location.hostname | location.host |
---|
It is a property of a location object. | It is also a property of a location object. |
It returns the domain name of the current webpage. | It returns the domain name as well as the port number (if available) of the current webpage. |
The return value is of String type. | The return value is of String type. |
Ex: www.abc.com | Ex: www.abc.com:8080 |
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics