CodePark/ questions / Unicode & text
START PRACTICINGPRACTICE
Frontend question · Internationalization › Unicode & text

Both strings look like "café" on screen. What does this print?

const a = "caf\u00E9";   // é as one character
const b = "cafe\u0301";  // e + combining accent
console.log(a === b, a.length, b.length);
console.log(a.normalize("NFC") === b.normalize("NFC"));
false 4 5 and then true

Unicode can write "é" in two ways: as one precomposed character (U+00E9), or as "e" followed by a combining accent (U+0301). They look the same but are different code units, so === is false and the lengths differ. normalize("NFC") turns both into the same precomposed form, so they become equal.

"
true 4 4" is the tempting wrong answer, because the strings look identical. Text from macOS file names, copy and paste or other systems can arrive in either form. Normalize user input before you compare it, search it or use it as a key.