Skip to content

Commit 8f2a05c

Browse files
nodeavtargos
authored andcommitted
doc: explain edge case when assigning port to url
numbers which are coerced to scientific notation via .toString(), will behave unexpectedly when assigned to a url's port. Fixes: #19595 PR-URL: #19645 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 8d2ca73 commit 8f2a05c

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

doc/api/url.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,9 @@ myURL.port = 1234.5678;
325325
console.log(myURL.port);
326326
// Prints 1234
327327

328-
// Out-of-range numbers are ignored
329-
myURL.port = 1e10;
328+
// Out-of-range numbers which are not represented in scientific notation
329+
// will be ignored.
330+
myURL.port = 1e10; // 10000000000, will be range-checked as described below
330331
console.log(myURL.port);
331332
// Prints 1234
332333
```
@@ -336,9 +337,25 @@ in the range `0` to `65535` (inclusive). Setting the value to the default port
336337
of the `URL` objects given `protocol` will result in the `port` value becoming
337338
the empty string (`''`).
338339

339-
If an invalid string is assigned to the `port` property, but it begins with a
340-
number, the leading number is assigned to `port`. Otherwise, or if the number
341-
lies outside the range denoted above, it is ignored.
340+
Upon assigning a value to the port, the value will first be converted to a
341+
string using `.toString()`.
342+
343+
If that string is invalid but it begins with a number, the leading number is
344+
assigned to `port`.
345+
Otherwise, or if the number lies outside the range denoted above,
346+
it is ignored.
347+
348+
Note that numbers which contain a decimal point,
349+
such as floating-point numbers or numbers in scientific notation,
350+
are not an exception to this rule.
351+
Leading numbers up to the decimal point will be set as the URL's port,
352+
assuming they are valid:
353+
354+
```js
355+
myURL.port = 4.567e21;
356+
console.log(myURL.port);
357+
// Prints 4 (because it is the leading number in the string '4.567e21')
358+
```
342359

343360
#### url.protocol
344361

0 commit comments

Comments
 (0)