Handle Unicode text, emoji, encoding, normalization, code points, and safe storage.
Lessons
Detect and reject invalid byte sequences.
Validating UTF-8 is easiest to learn by reading the example, changing it, and observing the result.
// Reject text that is not valid UTF-8 / contains lone surrogates
function isWellFormed(text) {
// Modern engines: String.prototype.isWellFormed()
if (text.isWellFormed) return text.isWellFormed();
try { encodeURIComponent(text); return true; }
catch { return false; }
}
console.log(isWellFormed('Hello 😀')); // true
console.log(isWellFormed('\uD800')); // false (lone surrogate)Practice the Validating UTF-8 example in a small scratch file, then explain what changed and why.