JavaScript (ES2026) reference · String Methods
JavaScript String toWellFormed() Method
Return a copy where lone surrogates are replaced with U+FFFD, so the string can be encoded safely.
JavaScript (ES2026) Since ES2024
Syntax
string.toWellFormed()
Return value
A new, well-formed string.
Examples
const bad = "ab\uD800";
console.log(bad.isWellFormed(), bad.toWellFormed().isWellFormed());
Output
false true
try {
encodeURIComponent("\uD800");
} catch (e) {
console.log(e.name);
}
console.log(encodeURIComponent("\uD800".toWellFormed()));
Output
URIError %EF%BF%BD
Version history
| Version | Change |
|---|---|
| JavaScript ES2024 | Added |
Related
string.isWellFormed()· Return true if the string has no lone surrogates, i.e. it can be safely encoded as UTF-8.string.normalize()· Return the Unicode normalization form of a string so visually identical text compares equal (NFC, NFD, NFKC, NFKD).- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial