location.hostname returns the hostname of the current page. It is a property, not a function, so do not add parentheses.
const hostname = location.hostname;
console.log(hostname);
For this page, the current value is:
hostname contains only the hostname. host can also contain an explicit port.
console.log(location.hostname);
console.log(location.host);
The older example used document.write(). For normal page output, update an existing element instead.
const output = document.getElementById('hostnameOutput');
output.textContent = location.hostname;
JavaScript location Full URL Refresh the page
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.