Skip to content

Commit 9142935

Browse files
mcollinatargos
authored andcommitted
doc: clarify that new URL().port could be an empty string
PR-URL: #22232 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: George Adams <[email protected]>
1 parent 866965e commit 9142935

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

doc/api/url.md

+25-13
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,31 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
305305

306306
Gets and sets the port portion of the URL.
307307

308+
The port value may be a number or a string containing a number in the range
309+
`0` to `65535` (inclusive). Setting the value to the default port of the
310+
`URL` objects given `protocol` will result in the `port` value becoming
311+
the empty string (`''`).
312+
313+
The port value can be an empty string in which case the port depends on
314+
the protocol/scheme:
315+
316+
| protocol | port |
317+
| :------- | :--- |
318+
| "ftp" | 21 |
319+
| "file" | |
320+
| "gopher" | 70 |
321+
| "http" | 80 |
322+
| "https" | 443 |
323+
| "ws" | 80 |
324+
| "wss" | 443 |
325+
326+
Upon assigning a value to the port, the value will first be converted to a
327+
string using `.toString()`.
328+
329+
If that string is invalid but it begins with a number, the leading number is
330+
assigned to `port`.
331+
If the number lies outside the range denoted above, it is ignored.
332+
308333
```js
309334
const myURL = new URL('https://example.org:8888');
310335
console.log(myURL.port);
@@ -346,19 +371,6 @@ console.log(myURL.port);
346371
// Prints 1234
347372
```
348373

349-
The port value may be set as either a number or as a string containing a number
350-
in the range `0` to `65535` (inclusive). Setting the value to the default port
351-
of the `URL` objects given `protocol` will result in the `port` value becoming
352-
the empty string (`''`).
353-
354-
Upon assigning a value to the port, the value will first be converted to a
355-
string using `.toString()`.
356-
357-
If that string is invalid but it begins with a number, the leading number is
358-
assigned to `port`.
359-
Otherwise, or if the number lies outside the range denoted above,
360-
it is ignored.
361-
362374
Note that numbers which contain a decimal point,
363375
such as floating-point numbers or numbers in scientific notation,
364376
are not an exception to this rule.

0 commit comments

Comments
 (0)