String.prototype[Symbol.iterator]() returns an iterator that yields each Unicode code point as a string. That is what powers for...of, spread (...), and Array.from on strings (ES2015).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Iterate a string. Start with this line.
JavaScript
str[Symbol.iterator]()
Call
What it does
str[Symbol.iterator]()
Primary signature.
str[Symbol.iterator](...)
Call with the arguments your case needs.
Built into JavaScript. Strings are iterable via [Symbol.iterator]() β usually through for...of / spread.
Library declarations
JavaScript
str[Symbol.iterator]()
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
The value returned by str[Symbol.iterator]().
JavaScript Examples
Example 1 — for...of and Spread
The usual way to walk a string by Unicode code points.
JavaScript
const text = "Hiπ";
for (const ch of text) {
console.log(ch);
}
// H
// i
// π
console.log(JSON.stringify([...text]));
// ["H","i","π"]
Length counts units. Many emoji need two UTF-16 code units.
Spread uses the iterator. You get one array item for the emoji.
Index 0 is unsafe for emoji. It can return only half a surrogate pair.
Rule of thumb. Prefer for...of / spread when βcharactersβ mean code points.
Compatibility
Browser & Runtime Support
String.prototype[Symbol.iterator]() is part of ES2015 (ES6). It works in all modern browsers and Node.js versions in common use today.
✓ Baseline · ES2015
String[Symbol.iterator]()
Supported in Chrome 38+, Firefox 36+, Safari 9+, Edge 12+, and Node.js 4+. Not available in Internet Explorer without a polyfill.
98%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
[Symbol.iterator]Excellent
Bottom line: Safe for modern web apps and current Node LTS. for...of and spread on strings require the same ES2015 iterator support.
Frequently Asked Questions
It returns a string iterator that yields each Unicode code point as a string. That makes strings iterable for for...of, spread (...), Array.from, and similar APIs.
Rarely. for...of and the spread syntax call it automatically. Call it manually when you want step-by-step control with next().
str[i] and charAt(i) read UTF-16 code units. The iterator yields full Unicode code points, so a single emoji made of a surrogate pair is one step, not two.
Surrogate pairs stay together as one code point. Grapheme clusters (emoji + skin tone, ZWJ sequences) can still split into multiple code points.
An object { value, done }. value is the next code-point string (or undefined when done is true).
No. It only creates an iterator that reads the string. Strings are immutable.