Sub topic 1
What a JavaScript String Really Contains
A JavaScript string looks like a sequence of characters, but the engine stores something more specific: a sequence of 16-bit numbers called UTF-16 code units. Most string bugs around encoding come from treating code units, characters, and bytes as the same thing. This chapter separates them so the rest of the tutorial has a clear model to build on.
Prerequisites
This chapter assumes you can:
- Read modern JavaScript syntax, including
const, arrow functions, and spread (...). - Run snippets in a browser console or Node.js.
- Read hexadecimal numbers such as
0x41and know that a byte holds 8 bits.
Code Points and Code Units
Unicode gives every character a number called a code point, written as U+ followed by hex digits. The letter A is U+0041, € is U+20AC, and the emoji 😀 is U+1F600.
A 16-bit code unit can hold values up to 0xFFFF. Code points in that range, which include most Latin, Greek, Cyrillic, and CJK characters, fit in a single code unit. Code points above 0xFFFF, such as most emoji, are split into two code units called a surrogate pair: a high surrogate (0xD800–0xDBFF) followed by a low surrogate (0xDC00–0xDFFF).
What `length` Counts
length counts code units, not characters. Code point methods behave differently from code unit methods:
const s = "a€😀";
console.log(s.length); // 4 (code units)
console.log(s.charCodeAt(2).toString(16)); // "d83d" (high surrogate)
console.log(s.charCodeAt(3).toString(16)); // "de00" (low surrogate)
console.log(s.codePointAt(2).toString(16)); // "1f600" (full code point)
// for...of and spread iterate by code point
console.log([...s].length); // 3
for (const ch of s) {
console.log(ch, ch.codePointAt(0).toString(16));
}
// a 61
// € 20ac
// 😀 1f600| API | Unit it works in |
|---|---|
| `length`, indexing `s[i]`, `charCodeAt`, `slice` | UTF-16 code units |
| `codePointAt`, `String.fromCodePoint` | Code points |
| `for...of`, `[...s]`, `Array.from(s)` | Code points |
Common Errors
Slicing through a pair. "😀".slice(0, 1) returns only the high surrogate. That leaves a broken string, which shows up later as � once it is encoded.
Assuming one code point is one visible character. Some characters have more than one form. "é" can be the single code point U+00E9, or "e" followed by the combining accent U+0301. The two strings look identical but have different lengths:
const composed = "\u00E9";
const decomposed = "e\u0301";
console.log(composed.length, decomposed.length); // 1 2
console.log(composed === decomposed); // false
console.log(composed === decomposed.normalize("NFC")); // trueUsing length as a byte count. UTF-16 code units are an in-memory detail. Files, HTTP bodies, and most APIs use UTF-8, where a character takes 1 to 4 bytes. "héllo €" has a length of 7 but encodes to 10 UTF-8 bytes. The next chapter explains why.
Summary
A string is a list of UTF-16 code units. Characters above U+FFFF take two code units. Indexing, length, and slice work in code units. Iteration and codePointAt work in code points. Neither of those counts is the byte count you get after encoding.
Exercise
What do "€😀".length and [..."€😀"].length return, and why do they differ?
Show answer
"€😀".length returns 3 and [..."€😀"].length returns 2. € (U+20AC) fits in one code unit, and 😀 (U+1F600) needs a surrogate pair of two code units, so length counts 3. Spread iterates by code point, so it yields 2 elements.
Exercise
Write an expression that returns the code point of the last character of a string s, even when that character is an emoji.
Show answer
[...s].at(-1).codePointAt(0). Spreading splits the string by code point, so the last element is the whole character. s.codePointAt(s.length - 1) would return only the low surrogate.