DEV Community

KCoder
KCoder

Posted on

Iterating over emojis with for/of?

This will be a short one

In ES6 strings are by default iterable with a for/of loop character-by-character. When iterating over strings, they are iterated by the Unicode codepoint, not by UTF-16 characters.

This can leave room for unexpected behavior. Let's look at this string for example: "I๐Ÿงก๐Ÿ˜€".

let str = "I๐Ÿงก๐Ÿ˜€";

console.log(str.length); // => 5

for (let char of str) {
    console.log(char); // => "I", "๐Ÿงก", "๐Ÿ˜€"
}
Enter fullscreen mode Exit fullscreen mode

This string has a .length of 5 because the two emojis require each two UTF-16 characters to represent. But when you iterate over them with a for/of loop, the loop body will run three times, once for each codepoint "I", "๐Ÿงก", "๐Ÿ˜€".

Top comments (0)