THE WORLD'S LARGEST WEB DEVELOPER SITE

Location port Property

❮ Location Object

Example

Return the port number of the current URL:

var x = "Port: " + location.port;
Try it Yourself »

Definition and Usage

The port property sets or returns the port number the server uses for a URL.

Note: If the port number is not specified in the URL (or if it is the scheme's default port - like 80, or 443), some browsers will display 0 or nothing.


Browser Support

Property
port Yes Yes Yes Yes Yes

Syntax

Return the port property:

location.port

Set the port property:

location.port = port

Property Values

Value Type Description
port String Specifies the port number of the URL

Technical Details

Return Value: A String, representing the port number of a URL.

Note: If the port number is not specified or if it is the scheme's default port (like 80 or 443), an empty string is returned

❮ Location Object