The isWellFormed() method of String values returns a boolean indicating whether this string contains any lone surrogates.



Return value

Returns true if this string does not contain any lone surrogates, false otherwise.


Strings in JavaScript are UTF-16 encoded. UTF-16 encoding has the concept of surrogate pairs, which is introduced in detail in the UTF-16 characters, Unicode code points, and grapheme clusters section.

isWellFormed() allows you to test whether a string is well-formed (i.e. does not contain any lone surrogates). Compared to a custom implementation, isWellFormed() is more efficient, as engines can directly access the internal representation of strings. If you need to convert a string to a well-formed string, use the toWellFormed() method. isWellFormed() allows you to handle ill-formed strings differently from well-formed strings, such as throwing an error or marking it as invalid.


Using isWellFormed()

const strings = [
  // Lone high surrogate
  // Lone low surrogate
  // Well-formed

for (const str of strings) {
// Logs:
// false
// false
// false
// false
// true
// true

Avoiding errors in encodeURI()

encodeURI throws an error if the string passed is not well-formed. This can be avoided by using isWellFormed() to test the string before passing it to encodeURI().

const illFormed = "\uD800";

try {
} catch (e) {
  console.log(e); // URIError: URI malformed

if (illFormed.isWellFormed()) {
} else {
  console.warn("Ill-formed strings encountered."); // Ill-formed strings encountered.


Well-Formed Unicode Strings
# sec-string.prototype.iswellformed

Browser compatibility

BCD tables only load in the browser

See also