Window location.port

Example

Get the port number of the current URL:

let port = location.port;
Try it Yourself »

Description

The port property sets or returns the port number of a URL.

Note

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

See Also:

The location.host Property


Syntax

Return the port property:

location.port

Set the port property:

location.port = port

Property Value

Parameter Description
port The port number of the URL

Return Value

Type Description
A stringThe port number of a URL.

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

Browser Support

location.port is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.