Four letters, five code units, and a form that blames the wrong person.

An illustrative four-character form rejects José because its code counts five UTF-16 units.

Imagine finishing a signup form, entering your own name, and being told it is invalid.

You check for an accidental space.

You type it again.

Apparently, the software would prefer you to become someone else.

I find this kind of bug more interesting than another enormous architecture diagram because the code can look completely reasonable. Even the tests can pass, especially when everyone in the test suite happens to be called Alex.

So let’s give that form a slightly more interesting afternoon.

José is four letters. JavaScript has a follow up question.

Try these two strings in a JavaScript console. The escapes make the difference visible in the source, even though both strings display as José.

const composed = 'Jos\u00e9'; const decomposed = 'Jose\u0301'; console.log(composed); // José console.log(decomposed); // José console.log(composed.length); // 4 console.log(decomposed.length); // 5 console.log(composed === decomposed); // false 

One version contains a precomposed é. In the other, an e is followed by a combining acute accent.

Now imagine a deliberately tiny four-character limit. This is a teaching example, please don’t ship a four-character name field.

const fits = name => name.length <= 4; console.log(fits(composed)); // true console.log(fits(decomposed)); // false 

Same visible name. Different answer.

JavaScript’s string length property counts UTF-16 code units. It never promised to count the letters a person sees.

That distinction feels academic until a perfectly ordinary person cannot get through your form.

Two representations of José contain different code point counts but both form four grapheme clusters.

Before fixing the loop, decide what it is counting

My first question here is about the requirement. Does the limit protect a storage budget, describe a visible label, or constrain a particular string algorithm?

Those are different jobs, so they deserve different measurements.

For interview practice, the Google tagged distinct character problem gives you a useful starting point. Its published contract is printable ASCII, which makes ordinary character indexing appropriate. Solve that version first, then try changing the contract so a character means a grapheme cluster. Write down what happens to your window positions and duplicate keys before touching the loop.

Another small experiment starts with this Google tagged template replacement exercise. It also specifies ASCII. For your own extension, allow Unicode replacement values and substitute both versions of José into a greeting. Check that the inserted value survives unchanged. Both pages are tagged Google, while these Unicode variations are my additions to their original requirements.

I like these variations because they make the boundary visible. An ASCII solution can be correct for its question and still be the wrong helper to copy into a signup form.

Choose bytes for encoded payload size, code points for Unicode values, or grapheme clusters for user-perceived characters.

Counting what someone sees takes a different tool

If the product means user-perceived characters, grapheme clusters are a useful starting point. JavaScript provides Intl.Segmenter for segmentation, including this granularity.

const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' }); function graphemes(text) { return Array.from( segmenter.segment(text), item => item.segment ); } console.log(graphemes(composed).length); // 4 console.log(graphemes(decomposed).length); // 4 

Notice that spreading the string into an array would not solve the whole problem. That iterates code points, which can still be pieces of one visible character.

Take the woman-technologist emoji, for example.

const emoji = '👩‍💻'; console.log(emoji.length); // 5 code units console.log([...emoji].length); // 3 code points console.log(graphemes(emoji).length); // 1 cluster console.log(new TextEncoder().encode(emoji).length); // 11 bytes 

All four answers are useful. They just answer different questions.

For a profile preview, cutting through a grapheme cluster is an avoidable mess. This helper keeps whole clusters instead.

function preview(text, limit) { if (!Number.isInteger(limit) || limit < 0) { throw new RangeError('limit must be a non-negative integer'); } return graphemes(text).slice(0, limit).join(''); } console.log(preview('Hi 👩‍💻!', 4)); // Hi 👩‍💻 

Keep this kind of truncation for display previews. Silently shortening a person’s stored name would create a different problem while congratulating us for fixing the first one.

Also, a grapheme count is not a pixel-width measurement. A tidy character counter still cannot promise that a label fits inside its container.

Looking equal and comparing equal need separate decisions

Counting both versions of José correctly does not make their raw strings equal.

If your comparison should treat canonically equivalent text as equal, NFC normalization supplies a common representation.

const sameCanonicalText = (a, b) => a.normalize('NFC') === b.normalize('NFC'); console.log(sameCanonicalText(composed, decomposed)); // true console.log(sameCanonicalText('José', 'Jose')); // false 

That second result matters. Normalization is not permission to remove someone’s accent.

NFC maps canonically equivalent representations to the same form without changing é into e.

For a contact search, you might deliberately support accent-insensitive matching. For a display name, you might preserve the submitted form. Those decisions belong in the product behavior, rather than hiding inside a convenient utility named cleanString.

And please don’t turn a normalized name into a unique person identifier. Two people can share a name, and normalization does not solve visual lookalikes or account security.

Give the test suite a better guest list

Before closing the editor, add cases that challenge different assumptions rather than twenty more versions of Alex.

  • Both forms of José should count as four grapheme clusters. Keep their raw inequality and normalized equality as separate assertions.

  • A joined emoji should survive a preview intact. An output that merely contains no replacement symbol is not enough.

  • An empty string and an exact boundary should behave deliberately. Check zero, the permitted length, and one cluster beyond it.

  • A name should survive the actual save-and-load path. Compare the returned value with the preservation or normalization policy you chose.

Four regression checks cover equivalent spellings, joined emoji, boundary behavior, and round-trip preservation.

The first three fit in a tiny local test. The fourth needs your real application, because a correct frontend helper cannot prove that an API or database preserves the same contract.

console.assert(graphemes('').length === 0); console.assert(graphemes(composed).length === 4); console.assert(graphemes(decomposed).length === 4); console.assert(preview('Hi 👩‍💻!', 4) === 'Hi 👩‍💻'); console.assert(preview('Hi 👩‍💻!', 0) === ''); console.assert(sameCanonicalText(composed, decomposed)); console.assert(!sameCanonicalText('José', 'Jose')); 

For production, keep the client and server rules aligned, check support in your target runtimes, and enforce sensible request-size limits before expensive processing. A friendly visible character limit does not replace a byte budget. This little helper also materializes every cluster, so a large input service deserves a more careful implementation.

What I love about this exercise is that it changes the next code review. Someone writes length, substring, or unique characters, and suddenly there is a useful question to ask.

Which unit does the requirement mean?

You don’t need to memorize every Unicode rule to catch this bug. You need to stop treating the first measurement your language offers as the product requirement.

And honestly, if our software can handle a distributed job queue, it can make a little room for José.

~ Emily