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

String.prototype.toWellFormed() β€” mixed lone surrogate

πŸ“– 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() β€” Mixed String

In a string that contains both normal characters and a lone surrogate, only the surrogate is replaced.

Challenge: Implement fixMixed() that returns the well-formed version of 'ab\uD800cd'.

fixMixed() // β†’ 'abοΏ½cd'
πŸ’‘ Hints (1)
  • β€’ Only the lone surrogate is replaced β€” all other characters remain intact.