Why Do Some Emojis Count as Multiple Characters in JavaScript?
When debugging a web app, the author discovered that certain emojis occupy more than one character slot in JavaScript strings, revealing that emoji length varies because they are composed of multiple Unicode code points such as variation selectors and zero‑width joiners.
While fixing a bug in a web application, the author entered a few lines of JavaScript in the browser console and noticed that the length of some emojis behaved unexpectedly.
The bug involved a "Decompress" button that should fill a table with one character per cell. A football emoji (⚽️) followed by a single space caused an extra space to appear in the table, indicating that the emoji was counted as more than one character.
In reality, emojis are not uniformly one character long; some count as a single character, others as two, and some even as five, depending on their underlying Unicode representation.
The calendar emoji (📅) is a single Unicode code point (U+1F4C5), so using Array.from() on the string returns a length of 1.
The football emoji (⚽️) consists of two code points: U+26BD and U+FE0F. The latter is a Variation Selector‑16 that forces the emoji to be displayed in color; without it, the symbol appears in monochrome.
The family emoji (🧑🧑🧒) is more complex, composed of five code points: three individual person emojis (father, mother, child) and two Zero Width Joiner (ZWJ) characters that glue the separate symbols together into a single family icon.
These examples demonstrate that emojis are essentially combinations of Unicode code points, making it difficult to define a universal "character" length. While they appear as a single visual glyph, they may consist of multiple code points and array elements, so storage length does not always correspond to a single character.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
