Unicode guide
Character Count vs. Length: Code Units, Code Points, Graphemes, and Bytes
A text length is meaningful only when the unit is named. Code units, code points, graphemes, and encoded bytes describe different layers of the same string and can produce different totals.
Four useful ways to count
A grapheme is a user-perceived character. It can contain one code point or a sequence of code points that Unicode text segmentation treats as one visible unit. A code-point count instead records the individual Unicode values in the string.
JavaScript's .length property counts UTF-16 code units. A code point outside the Basic Multilingual Plane is represented by a surrogate pair and therefore adds two to .length, even when it participates in a single visible grapheme.
👩💻5 UTF-16 code units · 3 code points · 1 grapheme · 11 UTF-8 bytesThe woman and laptop emoji are joined by U+200D ZERO WIDTH JOINER; GlyphSift reports each count separately.
One appearance, different sequences
The visible letter é may be the precomposed code point U+00E9. It may also be stored as U+0065 LATIN SMALL LETTER E followed by U+0301 COMBINING ACUTE ACCENT.
Those forms can appear alike and form one grapheme, while still having different code-point, code-unit, and byte counts. Emoji joined with U+200D provide another common case in which several code points can form one grapheme.
Limits depend on the counter
Text fields and protocols may count code points, graphemes, UTF-16 code units, or UTF-8 bytes. The same text can therefore fit one limit and exceed another when the counters use different units.
In UTF-8, ASCII letters use one byte, many accented Latin letters use two, most CJK characters use three, and many emoji use four. A UTF-8 byte limit should be checked as bytes rather than inferred from visible length.
Primary standards