ProgramGuru

Working with Unicode Strings

Understand Working with Unicode Strings

Unicode text may contain characters represented by more than one UTF-16 code unit. A string’s length does not always equal the number of Unicode code points or visible grapheme clusters.

What you will learn: the purpose of working with unicode strings, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how working with unicode strings works and where its type rules apply.
  • Predict the exact output of the working with unicode strings example.
  • Diagnose a common issue: Do not slice arbitrary code-unit positions in emoji-rich text and assume you have complete characters.

Build the mental model

The spread operator or Array.from on a string iterates Unicode code points, so it counts a supplementary-plane emoji as one code point. Combining marks and joined emoji sequences can still be multiple code points but one visible grapheme. Intl.Segmenter can segment graphemes in supported runtimes.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Working with Unicode Strings: explained visuallyUTF-16 units: 2 → Code points: 1 → Visible glyph: 1UTF-16 units: 2Code points: 1Visible glyph: 1Follow the values through the operation
Working with Unicode Strings: UTF-16 units: 2 → Code points: 1 → Visible glyph: 1.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

const face = "😀";
console.log(face.length);
console.log([...face].length);
console.log(face.codePointAt(0)?.toString(16));

Verified output

2
1
1f600

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The emoji uses a surrogate pair, so face.length is 2 UTF-16 code units. Spreading iterates one Unicode code point, producing length 1. codePointAt(0) yields hexadecimal 1f600.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not slice arbitrary code-unit positions in emoji-rich text and assume you have complete characters. Code points also are not the same thing as user-visible graphemes.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Compare lengths for "a", "😀", and "é". Explain the differences between UTF-16 units and code points.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Why can "😀".length be 2?
Choose one answer. You can try again.

Answer and explanation

length counts UTF-16 code units, and this emoji uses two.

Unicode text may contain characters represented by more than one UTF-16 code unit. A string’s length does not always equal the number of Unicode code points or visible grapheme clusters.

Review the key idea

Working with Unicode Strings: Unicode text may contain characters represented by more than one UTF-16 code unit.

The emoji uses a surrogate pair, so face.length is 2 UTF-16 code units. Spreading iterates one Unicode code point, producing length 1. codePointAt(0) yields hexadecimal 1f600.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Objects in TypeScript →