Skip to content

Commit

Permalink
doc: clarify that new URL().port could be an empty string
Browse files Browse the repository at this point in the history
PR-URL: #22232
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
  • Loading branch information
mcollina committed Aug 17, 2018
1 parent 3814534 commit 2ce0380
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,31 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and

Gets and sets the port portion of the URL.

The port value may be a number or a string containing a number in the range
`0` to `65535` (inclusive). Setting the value to the default port of the
`URL` objects given `protocol` will result in the `port` value becoming
the empty string (`''`).

The port value can be an empty string in which case the port depends on
the protocol/scheme:

| protocol | port |
| :------- | :--- |
| "ftp" | 21 |
| "file" | |
| "gopher" | 70 |
| "http" | 80 |
| "https" | 443 |
| "ws" | 80 |
| "wss" | 443 |

Upon assigning a value to the port, the value will first be converted to a
string using `.toString()`.

If that string is invalid but it begins with a number, the leading number is
assigned to `port`.
If the number lies outside the range denoted above, it is ignored.

```js
const myURL = new URL('https://example.org:8888');
console.log(myURL.port);
Expand Down Expand Up @@ -346,19 +371,6 @@ console.log(myURL.port);
// Prints 1234
```

The port value may be set as either a number or as a string containing a number
in the range `0` to `65535` (inclusive). Setting the value to the default port
of the `URL` objects given `protocol` will result in the `port` value becoming
the empty string (`''`).

Upon assigning a value to the port, the value will first be converted to a
string using `.toString()`.

If that string is invalid but it begins with a number, the leading number is
assigned to `port`.
Otherwise, or if the number lies outside the range denoted above,
it is ignored.

Note that numbers which contain a decimal point,
such as floating-point numbers or numbers in scientific notation,
are not an exception to this rule.
Expand Down

0 comments on commit 2ce0380

Please sign in to comment.