r/learnjavascript • u/PangolinNo7928 • Oct 04 '26
Any 'inbuilt' way to iterate over string - with access to index AND option to break?
I've been trying to learn more about iterators - but it doesn't seem like there is a string equivalent of .entries()? myStr[Symbol.iterator].forEach() gives the index and value - but no option to break π
For veryyyyyy long strings is my best option for loops? Splitting to create an array? Writing a separate generator function? (assume I only need a single char/index per loop)
Thanks!
Edit - thanks to u/Psionatix for guiding me to Object.entries(myStr), which led me to Array.prototype.entries.call(myStr) - which has the benefit of returning the indexes as integers (vs strings in Object.entries()) Performance is also close enough to for loop for my purposes π
6
u/milan-pilan Oct 04 '26
Why not just a standard for-loop?
15
u/senocular Oct 04 '26 edited Oct 05 '26 •
Be careful with string iteration. There's some hidden complexities that are not immediately obvious and could have an impact depending on the kinds of strings you're working with.
A string's length and index-accessed characters go through (UTF-16) code units. This means characters like emojis will be seen as multiple characters.
This applies to characters in non-english alphabets as well
When iterating through a string using its built-in string iterator, it goes through unicode code points. This will see the previous emoji character as a single character making it a more preferred approach for string iteration.
However, it is not full proof as some characters consist of more than a single code point. Consider:
For these, you'll want to use the Segmenter which can separate strings by graphemes:
Note that while the segment object in this iteration contains an index property, that is a code unit index, not the segment index. So if there was another character in this string, its index would be 4, not 1, because the ππΎ character is 4 units long.
In these cases for the index you want, you'd need to count it off yourself. And you'd need to do this with the normal string iterator too, since its a values() iterator rather than an entries() iterator, and strings have no entries() (or keys(), or technically values()) of their own. As a workaround, you can use the forEach iterator helper which will internally count off and provide an index (numeric) for you
And if you go through the Array API to create an iterator, you'll be going through code units, not code points since array iteration is treating it as an array-like which looks at its
lengthand indexed properties.Internally its effectively doing the same thing as a
for (let i = 0; i < str.length; i++).Update - MDN references:
Iterator
1
u/redsandsfort Oct 04 '26
why not the simplest:
1
u/Psionatix Oct 04 '26
1
u/The_KOK_2511 helpful Oct 04 '26
Solo usa un
forde toda un vida y ya, losfor-eachson para cuando la iteraccion no depende del indice y se iterara sobre el 100% una vez en orden1
u/ITower__Education Oct 10 '26
Plain for...of works and you can break out of it. Just keep your own counter
It's lazy, so no array copy on huge strings.
One catch is that for...of walks code points, while Array.prototype.entries.call(str) and a classic for (let i...) loop walk UTF-16 units. They only match for plain text. With emoji, the indexes will differ.
If your text is plain, for (let i = 0; i < str.length; i++) is still the fastest))
-3
u/sirCoom Oct 04 '26
Would questions like this best be answered by AI? Genuine question. do we need posts like these anymore, whatβs yalls opinion