CKTIS / JavaScript
Strings: UTF-16, code points, and graphemes
A JavaScript string is an immutable sequence of UTF-16 code units. Learn how code units, code points, and graphemes differ, and why normalization matters when comparing text.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
text.length; text[index]; [...text]; text.normalize("NFC"); new Intl.Segmenter("en", { granularity: "grapheme" }).segment(text)A string value is immutable: text operations do not modify an existing value. length and indexing with text[index] refer to 16-bit UTF-16 code units, not user-perceived characters. A code point outside Unicode’s Basic Multilingual Plane, such as 😀, occupies a surrogate pair, or two code units. A string can also contain a lone surrogate, so it need not be well-formed UTF-16 text.
The string iterator, used by for...of and [...text], preserves valid surrogate pairs and iterates over code points. This still does not count graphemes: a grapheme approximates one user-perceived character and can contain several code points, such as a letter followed by a combining accent. split("") splits code units, not graphemes. For grapheme segmentation, use Intl.Segmenter with { granularity: "grapheme" }, specified in ECMA-402; check its availability in the target environment before use. A repository’s Node version does not establish browser support.
=== compares code-unit sequences without automatic normalization. Consequently, "\u00E9" and "e\u0301" are unequal even though they are canonically equivalent. normalize("NFC") allows comparison after canonical normalization. NFD, NFKC, and NFKD are also available; compatibility forms can remove distinctions important to an application. Normalization does not replace grapheme segmentation or case-insensitive comparison.
The example requires ECMAScript 2015: it uses the string iterator, spread syntax, and normalize. Its text contains four code units, three code points, and two graphemes. The example does not require Intl.Segmenter or Web APIs.
Reference identifiers covered by this entry: UTF-16, code points, graphemes and normalization.
Examples
const text = "\u{1F600}e\u0301";
const composed = "\u00E9";
const decomposed = "e\u0301";
console.log(text.length);
console.log([...text].length);
console.log(composed === decomposed);
console.log(composed.normalize("NFC") === decomposed.normalize("NFC"));Result
4 3 false true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.