JavaScript (ES2026) reference · String Methods
JavaScript String isWellFormed() Method
Return true if the string has no lone surrogates, i.e. it can be safely encoded as UTF-8.
JavaScript (ES2026) Since ES2024
Syntax
string.isWellFormed()
Return value
true or false.
Examples
console.log("ok \u{1F600}".isWellFormed(), "broken \uD83D".isWellFormed());
Output
true false
const input = "x\uDC00y";
const safe = input.isWellFormed() ? input : input.toWellFormed();
console.log(encodeURIComponent(safe));
Output
x%EF%BF%BDy
Version history
| Version | Change |
|---|---|
| JavaScript ES2024 | Added |
Related
string.toWellFormed()· Return a copy where lone surrogates are replaced with U+FFFD, so the string can be encoded safely.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