Why can a JavaScript string contain one visible emoji yet behave as though it contains two units? UTF-16 represents a Unicode code point with either one or two 16-bit code units. A code point above U+FFFF needs two units, called a surrogate pair. An operation that reads individual code units can therefore split what a reader sees as one symbol. section 2.1.2 section 2.3
To decode a surrogate pair, subtract 0xD800 from the high surrogate and multiply by 0x400. Add the low surrogate minus 0xDC00, then add 0x10000. For 0xD801 and 0xDC37, the pieces are 0x400 and 0x37, producing code point U+10437. This is why two 16-bit units do not simply mean two Unicode characters. section 2.1.2
A regular expression that splits an emoji
Given `const str = '😀ABC'`, `/^./.exec(str)` matches one surrogate rather than the whole emoji. `/^./u.exec(str)` uses Unicode-aware matching. The `u` flag also matters for Unicode ranges and property escapes. section 2.1.2 section 2.3
Keep the layers separate: UTF-16 is an encoding; a JavaScript string is a sequence of 16-bit unsigned integers that need not be valid UTF-16 text. UTF-16 uses two or four bytes per code point, while UTF-8 uses one to four. Neither encoding alone resolves visually identical strings built from different code points; normalize strings when that distinction matters. section 2.1.2 section 2.3
