The [@@iterator]()
method returns a new iterator object that iterates over the code points of a String value, returning each code point as a String value.
The [@@iterator]()
method returns a new iterator object that iterates over the code points of a String value, returning each code point as a String value.
str[Symbol.iterator]
A new iterator object.
A String is iterable because it implements the @@iterator
method. It means strings can be used in for...of
loops, be spread in arrays, etc.
Strings are iterated by Unicode codepoints. 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
const str = 'A\uD835\uDC68'; const strIter = str[Symbol.iterator](); console.log(strIter.next().value); // "A" console.log(strIter.next().value); // "\uD835\uDC68"
const str = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A'; for (const v of str) { console.log(v); } // "A" // "\uD835\uDC68" // "B" // "\uD835\uDC69" // "C" // "\uD835\uDC6A"
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
@@iterator |
38 |
12 |
36
27-36
A placeholder property named
@@iterator is used.17-27
A placeholder property named
iterator is used. |
No |
25 |
9 |
38 |
38 |
36
27-36
A placeholder property named
@@iterator is used.17-27
A placeholder property named
iterator is used. |
25 |
9 |
3.0 |
1.0 |
0.12.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator