URL: port property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

The port property of the URL interface is a string containing the port number of the URL, or the empty string if the port is the default for the protocol.

Note: If the URL object refers to a URL that doesn't contain an explicit port number (e.g., https://localhost) or contains a port number that's the default port number corresponding to the protocol part of the URL (e.g., https://localhost:443), then the value of the port property will be the empty string: ''.

Value

A string.

Examples

js
// https protocol with non-default port number
new URL("https://example.com:5443/svn/Repos/").port; // '5443'
// http protocol with non-default port number
new URL("http://example.com:8080/svn/Repos/").port; // '8080'
// https protocol with default port number
new URL("https://example.com:443/svn/Repos/").port; // '' (empty string)
// http protocol with default port number
new URL("http://example.com:80/svn/Repos/").port; // '' (empty string)
// https protocol with no explicit port number
new URL("https://example.com/svn/Repos/").port; // '' (empty string)
// http protocol with no explicit port number
new URL("https://example.com/svn/Repos/").port; // '' (empty string)
// ftp protocol with non-default port number
new URL("ftp://example.com:221/svn/Repos/").port; // '221'
// ftp protocol with default port number
new URL("ftp://example.com:21/svn/Repos/").port; // '' (empty string)

Specifications

Specification
URL Standard
# dom-url-port

Browser compatibility

BCD tables only load in the browser

See also

  • The URL interface it belongs to.