URL.prototype.host
Returns or sets the host portion of the URL including the port number if specified
Syntax
url.hostReturn Value
A string containing the host (hostname:port)
Examples
const url = new URL('https://example.com:8080/path')
console.log(url.host) // 'example.com:8080'const url = new URL('https://example.com/path')
console.log(url.host) // 'example.com' (no port means default)const url = new URL('http://localhost:3000/api')
url.host = 'localhost:8080'
console.log(url.href) // 'http://localhost:8080/api'Understanding URL.prototype.host
The URL.prototype.host method in JavaScript returns or sets the host portion of the URL including the port number if specified. It belongs to the URL object and is one of the most widely used methods for working with url values in modern JavaScript and TypeScript applications.
The method signature is url.host. When called, it returns a string containing the host (hostname:port). Understanding when and how to use host() helps you write more expressive, readable code.
Common use cases for URL.prototype.host include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like url-hostname, url-port, url-origin, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for URL.prototype.host is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More URL Methods
Other methods in the URL object
Related Tools
More URL Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.