Window location.port attribute

Definition and usage

port Sets or returns the port number of the URL.

Tip:If the port number is not specified in the URL, or it is the default port (80 for http) or (443 for https), most browsers will return an empty string.

See also:

location.host attribute

Example

Get the port number of the current URL:

let port = location.port;

Try it yourself

Syntax

Return the port attribute:

location.port

Set the port attribute:

location.port = port

Attribute value

Value Description
port The port number of the URL.

Return value

Type Description
String

The port number of the URL.

If the port number is not specified or it is the default port (such as 80 or 443), most browsers will return an empty string.

Browser Support

All Browsers Support location.port:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support