Modern JavaScript Web Development Cookbook
上QQ阅读APP看书,第一时间看更新

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"]