JS Built-ins
← String166 / 195
StringString.prototype.toWellFormedintermediate

String.prototype.toWellFormed() β€” normal string unchanged

πŸ“– Usage Example
'hello'.toWellFormed()      // β†’ 'hello'
'πŸ˜€'.toWellFormed()         // β†’ 'πŸ˜€'
'οΏ½'.toWellFormed()     // β†’ 'οΏ½'

Use toWellFormed() to replace lone surrogate code units with the Unicode replacement character U+FFFD (ES2024).

String.prototype.toWellFormed()

str.toWellFormed() returns a new string where lone surrogates are replaced with the Unicode replacement character U+FFFD ('\uFFFD'). A well-formed string is returned unchanged.

Challenge: Implement ensureWellFormed(str) that calls toWellFormed().

ensureWellFormed('hello') // β†’ 'hello'
πŸ’‘ Hints (1)
  • β€’ Strings without lone surrogates are returned as-is.