Iterating over strings

Strings are now iterable objects (such as arrays are), meaning that you can use for...of to iterate over them, character by character:

for (let ch of "PACKT") {
console.log(ch);
}

The spread operator (read about it in depth, in the Spreading and joining values section of this chapter) will also work, hence transforming a string into an array of single characters:

let letters = [..."PACKT"];
// ["P", "A", "C", "K", "T"]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset