Dev Helpers

JavaScript / Iterators and generators

Iterate Unicode safely with for-of

Dev Helpers ยท September 16, 2026

String length counts UTF-16 units. A for-of loop advances by Unicode code point, which keeps a supplementary character together.

Worked example

for (const char of "A๐Ÿ˜€") console.log(char);

Result

A
๐Ÿ˜€

Implementation notes

A cursor that advances one code point at a time can still land inside a visible character. For editing selections, compare the result with a grapheme-aware segmenter.

Try it yourself

Compare an emoji with a family emoji. The second still contains several code points.

Open the JavaScript example โ†’

Editorial date September 16, 2026 ยท Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.