The JavaScript location port property is used to get the port number of the current URL. It does not take any arguments and returns a string value representing the port number of the URL.
Here are some examples of how the port property can be used in JavaScript code:
- To get the port number of the current URL, you can use the following code:
// get the port number of the current URL
const port = location.port;
- To log the port number of the current URL to the console, you can use the following code:
// log the port number of the current URL to the console
console.log('Current URL port number:', location.port);
- To compare the port number of the current URL with a certain value, you can use the following code:
// define the port number to compare
const port = '8080';
// compare the port number of the current URL with the specified value
if (location.port === port) {
// do something...
}
Overall, the port property provides a convenient way to get the port number of the current URL. It can be useful for tasks such as checking the current server port or redirecting to a different port, or for other operations that require accessing the port number of the URL.
Leave a Reply