Open In App

JavaScript location.host vs location.hostname

Last Updated : 10 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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,

https://www.geeksforgeeks.org:8080/ds/heap

When we call location.hostname, This will return.

www.geeksforgeeks.org

Example :

Javascript




console.log("hostname : " + location.hostname);


Output:

hostname : ide.geeksforgeeks.org

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,

https://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);


Output:

host : ide.geeksforgeeks.org
                    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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads