Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions 1-js/05-data-types/06-iterable/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Arrays and strings are most widely used built-in iterables.
For a string, `for..of` loops over its characters:

```js run
for(let char of "test") {
for (let char of "test") {
alert( char ); // t, then e, then s, then t
}
```
Expand All @@ -130,16 +130,16 @@ And it works right with surrogate pairs!

```js run
let str = '𝒳😂';
for(let char of str) {
alert(char); // 𝒳, and then 😂
for (let char of str) {
alert( char ); // 𝒳, and then 😂
}
```

## Calling an iterator explicitly

Normally, internals of iterables are hidden from the external code. There's a `for..of` loop, that works, that's all it needs to know.

But to understand things a little bit more deeper let's see how to create an iterator explicitly.
But to understand things a little bit deeper let's see how to create an iterator explicitly.

We'll iterate over a string the same way as `for..of`, but with direct calls. This code gets a string iterator and calls it "manually":

Expand All @@ -151,7 +151,7 @@ let str = "Hello";

let iterator = str[Symbol.iterator]();

while(true) {
while (true) {
let result = iterator.next();
if (result.done) break;
alert(result.value); // outputs characters one by one
Expand Down Expand Up @@ -184,7 +184,7 @@ let arrayLike = { // has indexes and length => array-like

*!*
// Error (no Symbol.iterator)
for(let item of arrayLike) {}
for (let item of arrayLike) {}
*/!*
```

Expand Down Expand Up @@ -258,7 +258,7 @@ Technically here it does the same as:
let str = '𝒳😂';

let chars = []; // Array.from internally does the same loop
for(let char of str) {
for (let char of str) {
chars.push(char);
}

Expand Down