Skip to content

Commit 7c2fdca

Browse files
authored
Reference for stage 3 iterator-join (#44886)
* Reference for stage 3 iterator-join * Consistent descriptions
1 parent 453a2a2 commit 7c2fdca

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

files/en-us/web/javascript/reference/global_objects/array/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ These properties are own properties of each `Array` instance.
326326
- {{jsxref("Array.prototype.indexOf()")}}
327327
- : Returns the first (least) index at which a given element can be found in the calling array.
328328
- {{jsxref("Array.prototype.join()")}}
329-
- : Joins all elements of an array into a string.
329+
- : Returns a new string that is the concatenation of all elements in this array, separated by commas or a specified separator string.
330330
- {{jsxref("Array.prototype.keys()")}}
331331
- : Returns a new [_array iterator_](/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators) that contains the keys for each index in the calling array.
332332
- {{jsxref("Array.prototype.lastIndexOf()")}}

files/en-us/web/javascript/reference/global_objects/array/join/index.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ browser-compat: javascript.builtins.Array.join
77
sidebar: jsref
88
---
99

10-
The **`join()`** method of {{jsxref("Array")}} instances creates and
11-
returns a new string by concatenating all of the elements in this array,
12-
separated by commas or a specified separator string. If the array has
13-
only one item, then that item will be returned without using the separator.
10+
The **`join()`** method of {{jsxref("Array")}} instances returns a new string that is the concatenation of all elements in this array, separated by commas or a specified separator string. If the array has only one item, that item's stringification is returned without using the separator.
1411

1512
{{InteractiveExample("JavaScript Demo: Array.prototype.join()")}}
1613

files/en-us/web/javascript/reference/global_objects/iterator/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ You will find many iterator methods analogous to array methods, such as:
8585
| {{jsxref("Iterator.prototype.flatMap()")}} | {{jsxref("Array.prototype.flatMap()")}} |
8686
| {{jsxref("Iterator.prototype.forEach()")}} | {{jsxref("Array.prototype.forEach()")}} |
8787
| {{jsxref("Iterator.prototype.includes()")}} | {{jsxref("Array.prototype.includes()")}} |
88+
| {{jsxref("Iterator.prototype.join()")}} | {{jsxref("Array.prototype.join()")}} |
8889
| {{jsxref("Iterator.prototype.map()")}} | {{jsxref("Array.prototype.map()")}} |
8990
| {{jsxref("Iterator.prototype.reduce()")}} | {{jsxref("Array.prototype.reduce()")}} |
9091
| {{jsxref("Iterator.prototype.some()")}} | {{jsxref("Array.prototype.some()")}} |
@@ -175,6 +176,8 @@ These properties are defined on `Iterator.prototype` and shared by all `Iterator
175176
- : Executes a provided function once for each element produced by the iterator.
176177
- {{jsxref("Iterator.prototype.includes()")}} {{experimental_inline}}
177178
- : Returns `true` if an element produced by the iterator is equal to the given value. Otherwise, if the iterator is exhausted without finding such an element, it returns `false`.
179+
- {{jsxref("Iterator.prototype.join()")}}
180+
- : Returns a string that is the concatenation of all elements produced by the iterator, separated by commas or a specified separator string.
178181
- {{jsxref("Iterator.prototype.map()")}}
179182
- : Returns a new iterator helper object that yields elements of the iterator, each transformed by a mapping function.
180183
- {{jsxref("Iterator.prototype.reduce()")}}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: Iterator.prototype.join()
3+
short-title: join()
4+
slug: Web/JavaScript/Reference/Global_Objects/Iterator/join
5+
page-type: javascript-instance-method
6+
browser-compat: javascript.builtins.Iterator.join
7+
sidebar: jsref
8+
---
9+
10+
The **`join()`** method of {{jsxref("Iterator")}} instances is similar to {{jsxref("Array.prototype.join()")}}: it returns a string that is the concatenation of all elements produced by the iterator, separated by commas or a specified separator string. If the iterator has only one item, that item's stringification is returned without using the separator.
11+
12+
## Syntax
13+
14+
```js-nolint
15+
join()
16+
join(separator)
17+
```
18+
19+
### Parameters
20+
21+
- `separator` {{optional_inline}}
22+
- : A string to separate each pair of adjacent elements of the iterator. If omitted, the elements are separated with a comma (",").
23+
24+
### Return value
25+
26+
A string joining all yielded elements. The elements are [converted to strings](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion). If an element is `undefined` or `null`, it is converted to an empty string instead of the string `"null"` or `"undefined"`. If the iterator is empty, the empty string is returned.
27+
28+
## Description
29+
30+
See {{jsxref("Array.prototype.join()")}} for details about how `join()` works. Unlike most other iterator helper methods, it does not work well with infinite iterators, because it is not lazy.
31+
32+
## Examples
33+
34+
### Using join()
35+
36+
```js
37+
function* fibonacci() {
38+
let current = 1;
39+
let next = 1;
40+
while (true) {
41+
yield current;
42+
[current, next] = [next, current + next];
43+
}
44+
}
45+
46+
console.log(fibonacci().take(5).join()); // "1,1,2,3,5"
47+
console.log(fibonacci().take(5).join(" - ")); // "1 - 1 - 2 - 3 - 5"
48+
```
49+
50+
## Specifications
51+
52+
{{Specifications}}
53+
54+
## Browser compatibility
55+
56+
{{Compat}}
57+
58+
## See also
59+
60+
- [Polyfill of `Iterator.prototype.join` in `core-js`](https://github.com/zloirock/core-js#iterator-join)
61+
- [es-shims polyfill of `Iterator.prototype.join`](https://www.npmjs.com/package/es-iterator-helpers)
62+
- {{jsxref("Iterator")}}
63+
- {{jsxref("Iterator.prototype.reduce()")}}
64+
- {{jsxref("Array.prototype.join()")}}

files/en-us/web/javascript/reference/global_objects/typedarray/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ These methods are defined on the `TypedArray` prototype object and are thus shar
252252
- {{jsxref("TypedArray.prototype.indexOf()")}}
253253
- : Returns the first (least) index of an element within the array equal to the specified value, or `-1` if none is found. See also {{jsxref("Array.prototype.indexOf()")}}.
254254
- {{jsxref("TypedArray.prototype.join()")}}
255-
- : Joins all elements of an array into a string. See also {{jsxref("Array.prototype.join()")}}.
255+
- : Returns a new string that is the concatenation of all elements in this typed array, separated by commas or a specified separator string. See also {{jsxref("Array.prototype.join()")}}.
256256
- {{jsxref("TypedArray.prototype.keys()")}}
257257
- : Returns a new array iterator that contains the keys for each index in the array. See also {{jsxref("Array.prototype.keys()")}}.
258258
- {{jsxref("TypedArray.prototype.lastIndexOf()")}}

files/en-us/web/javascript/reference/global_objects/typedarray/join/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ browser-compat: javascript.builtins.TypedArray.join
77
sidebar: jsref
88
---
99

10-
The **`join()`** method of {{jsxref("TypedArray")}} instances creates and returns a new string by concatenating all of the elements in this typed array, separated by commas or a specified separator string. If the typed array has only one item, then that item will be returned without using the separator. This method has the same algorithm as {{jsxref("Array.prototype.join()")}}.
10+
The **`join()`** method of {{jsxref("TypedArray")}} instances returns a new string that is the concatenation of all elements in this typed array, separated by commas or a specified separator string. If the typed array has only one item, that item's stringification is returned without using the separator. This method has the same algorithm as {{jsxref("Array.prototype.join()")}}.
1111

1212
{{InteractiveExample("JavaScript Demo: TypedArray.prototype.join()")}}
1313

0 commit comments

Comments
 (0)