Surrogate Pairs and the BMP
You'll learn how JavaScript represents characters beyond the Basic Multilingual Plane using special two-unit sequences called surrogate pairs.
What's the Basic Multilingual Plane?
The Basic Multilingual Plane (BMP) is Unicode's first 65,536 characters (code points U+0000 to U+FFFF). This covers most common characters: Latin letters, numbers, common symbols, and many scripts like Chinese, Japanese, and Arabic.
But Unicode has over a million possible code points! Characters beyond U+FFFF live outside the BMP — these include emojis, ancient scripts, and rare symbols.
The Surrogate Pair Solution
JavaScript strings are sequences of 16-bit code units (remember from the last lesson). A single code unit can only hold values from 0 to 65,535 — exactly the BMP range.
For characters outside the BMP, JavaScript uses a clever trick: surrogate pairs. It represents one character using two code units working together:
- A high surrogate (first code unit: U+D800 to U+DBFF)
- A low surrogate (second code unit: U+DC00 to U+DFFF)
Think of it like a two-part ticket: neither half works alone, but together they grant access to characters beyond the basic range.
Example: The Emoji Problem
The 😀 emoji (grinning face) has code point U+1F600 — outside the BMP. JavaScript stores it as two code units: "\uD83D\uDE00". Your .length will report 2, even though you see one character.
Characters in the BMP (like "A" at U+0041) need only one code unit, so .length reports 1.
Key Takeaway: Characters beyond the Basic Multilingual Plane require two code units (a surrogate pair) in JavaScript strings, which is why some "single characters" have a .length of 2.