iteration

String.prototype[Symbol.iterator]

Returns an iterator that yields the string's Unicode code points as single-character strings, correctly handling surrogate pairs. This is what powers for...of loops and spread over strings.

str[Symbol.iterator]()

Parameters

Parameter Purpose
(no arguments) Called implicitly by for...of and spread
returns Iterator yielding one code-point string per step

Examples

console.log([...'abc']);

Prints [ 'a', 'b', 'c' ]

console.log([...'a😀b']);

Prints [ 'a', '😀', 'b' ] (surrogate pair joined)

for (const ch of 'hi') console.log(ch);

Prints 'h' then 'i' on two lines

const it = 'ab'[Symbol.iterator](); console.log(it.next().value);

Prints 'a'

Gotcha

Iterates by code point, unlike indexed access or .length which use UTF-16 code units — [...str].length is often the right way to count characters.

Related methods

← All JS string methods · JS array methods