String.prototype[Symbol.iterator]()
The Symbol.iterator
method of a string implements the iterable protocol and allows strings to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of
loops. It returns an iterator that yields the Unicode code points of the string value as individual strings.
Syntax
string[Symbol.iterator]()
Return value
A new iterable iterator object that yields the Unicode code points of the string value as individual strings.
Description
Strings are iterated by Unicode code points. This means grapheme clusters will be split, but surrogate pairs will be preserved.
// "Backhand Index Pointing Right: Dark Skin Tone"
[..."👉🏿"]; // ['👉', '🏿']
// splits into the basic "Backhand Index Pointing Right" emoji and
// the "Dark skin tone" emoji
// "Family: Man, Boy"
[..."👨👦"]; // [ '👨', '', '👦' ]
// splits into the "Man" and "Boy" emoji, joined by a ZWJ