Home > database >  When does string become an iterable object?
When does string become an iterable object?

Time:10-15

I read javascript.info. And I can't get something. The text is saying that strings are iterable. And here I can read that iterables are objects that implement the Symbol.iterator method.

So, when and how string becomes an iterable object? Does it mean that for..of turns primitive string into the “object wrapper”? Am I right?

CodePudding user response:

Yes, the String class does have a Symbol.iterator method, and you can use for..of on a string to produce the results as in this example:

var myString = "So, when and how string becomes an iterable object?";
for (let char of myString) {
  console.log(char);
}

  • Related