liveBook · Manning

Normalization

As explained in JavaScript in Depth

The process of converting equivalent Unicode character sequences into a standardized form.

Two strings can display as “café” yet fail `===`. The final é can be one code unit, U+00E9, or an e followed by a combining accent, U+0065 U+0301. JavaScript compares the resulting UTF-16 code-unit sequences, not their appearance. Their `length` values can differ too: four code units versus five. section 2.1.2

Compare equivalent spellings

const a = 'caf\u00E9'; const b = 'cafe\u0301'; a === b; // false a.normalize() === b.normalize(); // true Normalize both operands before comparing them. `normalize()` defaults to NFC, which produces the composed form here. section 2.1.2

Table 2.4 Unicode normalization forms
Form
Name
Description
Example Input
Result
Code Units
NFC
Canonical Composition
Decomposes then recombines characters
café (e + ́)
café
U+0063 U+0061 U+0066 U+00E9
NFD
Canonical Decomposition
Separates base characters from combining marks (like accents)
café
café (e + ́)
U+0063 U+0061 U+0066 U+0065 U+0301
NFKC
Compatibility Composition
Like NFC but also normalizes variants
fi (ligature, one symbol)
fi (two symbols)
U+0066 U+0069
NFKD
Compatibility Decomposition
Like NFD but also normalizes variants
½ (one symbol)
1⁄2 (three symbols)
U+0031 U+2044 U+0032

The four forms make different choices. NFC composes canonically equivalent sequences; NFD separates base characters and combining marks. NFKC and NFKD also normalize compatibility variants, such as the fi ligature or ½. Choose a form deliberately, then use it consistently. section 2.1.2

function registerUser (username) {
    const normalized = username.normalize();
    if (existingUsers.some(u => u.normalize() === normalized)) {
        return 'Username already taken';
    }
    existingUsers.push(username);
    return 'Registration successful';
}

A username check can miss an existing, visually identical name when their code-unit sequences differ. Normalize the incoming name and each stored name before testing equality. section 2.1.2

How JavaScript in Depth covers it

How deep it goes
Explains the UTF-16 code-unit mismatch, distinguishes four normalization forms, and applies `normalize()`; does not detail the normalization algorithm.
Examples
Contrasts two encodings of café, tabulates four forms, and repairs a username-registration duplicate check.
What it assumes
Assumes basic JavaScript string comparison while explaining UTF-16 code units and combining-character differences.
What you can do after
Apply it via JavaScript `normalize()` to compare canonically equivalent strings consistently.
How it connects
Connects normalization to UTF-16, combining characters, string equality, string length, and Unicode compatibility variants.

Also explained in

Compare how 24 books explain normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.