Iterables
Iterables
A generalization of arrays, it allows us to make any object iterable in the context of for ... of
loop.
Making an object iterable just requires you to add the Symbol.iterator
hidden property which is a function that returns the iterator. Really doubt this is ever need to be done, but the option is there if it is needed. The code for it isn't that bad.
String is iterable
You can iterate over each letter of a String using for ... of
loop since the String type probably have Symbol.iterator
implemented.
for (let char of "Testing") {
console.log(char); // T, e, s, t, i, n, g
}
No Comments