String.prototype.toWellFormed() returns a new string where every lone UTF-16 surrogate is replaced with U+FFFD (�). Well-formed text (including valid emoji pairs) is left intact. It takes no parameters (ES2024).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.toWellFormed. Start with this line.
JavaScript
string.toWellFormed()
Call
What it does
string.toWellFormed()
Primary signature.
string.toWellFormed(...)
Call with the arguments your case needs.
Built into JavaScript. Call toWellFormed(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
string.toWellFormed()
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
New well-formed string (lone surrogates → �)
JavaScript Examples
Example 1 — Repair Lone Surrogates
Well-formed text stays; orphan surrogates become �.
Raw encode fails.encodeURI rejects lone surrogates → URIError.
Repair first.toWellFormed() swaps the orphan for �.
Encode succeeds.� encodes as %EF%BF%BD.
Rule of thumb. Sanitize untrusted UTF-16 before URI APIs.
Compatibility
Browser & Runtime Support
String.prototype.toWellFormed() is part of ES2024. It reached Baseline Widely available around October 2023.
✓ Baseline · ES2024
String.prototype.toWellFormed()
Supported in Chrome 111+, Firefox 119+, Safari 16.4+, Edge 111+, and Node.js 20+. Not available in Internet Explorer.
90%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
toWellFormed()Excellent
Bottom line: Safe for modern web apps and Node 20+. For older targets, feature-detect with if ("toWellFormed" in String.prototype) and polyfill, or skip repair when unsupported.
Frequently Asked Questions
toWellFormed() returns a new string where every lone UTF-16 surrogate is replaced with the Unicode replacement character U+FFFD (�). Well-formed strings keep the same text content.
UTF-16 uses pairs of code units for characters above U+FFFF. A lone (orphan) surrogate is one half without its matching partner — for example "\uD800" alone.
No. Call it with empty parentheses: str.toWellFormed().
isWellFormed() returns true or false. toWellFormed() returns a repaired string. Check with isWellFormed(); fix with toWellFormed().
encodeURI throws URIError on ill-formed strings. Passing str.toWellFormed() replaces lone surrogates first so encoding succeeds (the replacement becomes %EF%BF%BD).
No. Strings are immutable. It always returns a new string — even when the input was already well-formed.