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.

ITPUB
ITPUB
ITPUB
Why Do Some Emojis Count as Multiple Characters in JavaScript?

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.

在浏览器的控制台里敲下的几行 JavaScript 代码
在浏览器的控制台里敲下的几行 JavaScript 代码
有人看出这是哪个 FC 游戏的画面了吗
有人看出这是哪个 FC 游戏的画面了吗
ZWJ 相当于 emoji 世界的“胶水”
ZWJ 相当于 emoji 世界的“胶水”
frontendemojiJavaScriptUnicodestring length
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.