transformation
String.prototype.normalize
Returns the Unicode Normalization Form of the string ('NFC', 'NFD', 'NFKC', or 'NFKD'; default 'NFC'). Essential for reliable comparison of characters that can be represented in multiple code point sequences.
str.normalize([form]) Parameters
| Parameter | Purpose |
|---|---|
| form | 'NFC' (default), 'NFD', 'NFKC', or 'NFKD' |
| returns | New normalized string; RangeError on invalid form |
Examples
const a = '\u00F1'; // 'ñ' (single NFC codepoint)
const b = 'n\u0303'; // 'ñ' (n + combining tilde, NFD)
console.log(a === b); // false — different code points
console.log(a.normalize() === b.normalize()); // true — both NFC after normalize() The two literals render identically but are different code point sequences. normalize() (defaulting to NFC) reconciles them.
console.log('ñ'.normalize('NFD') === 'ñ'.normalize('NFD')); Prints true
console.log('ñ'.normalize('NFD').length); Prints 2
console.log('①'.normalize('NFKC')); Prints '1'
Gotcha
An invalid form argument throws a RangeError. NFKC/NFKD apply compatibility mappings and can drastically alter characters — use NFC for most comparison work.
Related methods
String.prototype.localeCompare
Returns a negative number, 0, or a positive number indicating whether the string sorts before, equal to, or after compareString according to the current or supplied locale. Preferred over < and > for user-visible sorting.
String.prototype.toLowerCase
Returns a new string with all characters converted to lower case using Unicode default case mapping. For locale-specific behavior use toLocaleLowerCase.
String.prototype.codePointAt
Returns the full Unicode code point (0–0x10FFFF) starting at the given position, correctly combining surrogate pairs. Added in ES2015 as the Unicode-safe counterpart to charCodeAt.
String.prototype.replace
Returns a new string with the first match of pattern replaced. Pattern can be a string (only the first occurrence) or a RegExp (all occurrences when the g flag is set).
String.prototype.replaceAll
Returns a new string with every occurrence of pattern replaced. Added in ES2021; if pattern is a RegExp it must have the g flag or replaceAll throws TypeError.