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
String.prototype.codePointAt
Returns the full Unicode code point (0–0x10FFFF) starting at the given position, correctly combining surrogate pairs. Added in ES2015 as the Unicode-safe counterpart to charCodeAt.
String.prototype.length
Read-only property giving the number of UTF-16 code units in the string. Not the number of visible characters — emoji and combining marks can inflate the count.
String.prototype.split
Divides the string into an array of substrings using separator (string or RegExp). With no separator the whole string is returned as a one-element array; with '' each UTF-16 code unit becomes an element.