The isWellFormed() method of the String class in JavaScript returns a boolean indicating whether the string contains any lone surrogates. Lone surrogates are individual code units that do not form a complete UTF-16 character and are considered ill-formed.
The syntax of String.isWellFormed() method is:
isWellFormed()
This isWellFormed() method of String returns a boolean indicating whether this string contains any lone surrogates.
String.isWellFormed() returns value of type Boolean
.
In this example, we use the isWellFormed()
method to check if a string without any lone surrogates is well-formed.
For example,
str
with the value 'Hello'
.isWellFormed()
method on str
to check if it is well-formed.true
, is stored in the variable isWellFormedString
.isWellFormedString
to the console using console.log()
method.const str = 'Hello';
const isWellFormedString = str.isWellFormed();
console.log(isWellFormedString);
true
This example demonstrates using the isWellFormed()
method to check if a string containing lone surrogates is well-formed.
For example,
str
with a lone surrogate '\uD800'
.isWellFormed()
method on str
to check if it is well-formed.false
, is stored in the variable isWellFormedString
.isWellFormedString
to the console using console.log()
method.const str = '\uD800';
const isWellFormedString = str.isWellFormed();
console.log(isWellFormedString);
false
In this example, we use the isWellFormed()
method to check if a string containing emojis is well-formed.
For example,
str
with the value '😊'
.isWellFormed()
method on str
to check if it is well-formed.true
, is stored in the variable isWellFormedString
.isWellFormedString
to the console using console.log()
method.const str = '😊';
const isWellFormedString = str.isWellFormed();
console.log(isWellFormedString);
true
In this JavaScript tutorial, we learned about isWellFormed() method of String: the syntax and few working examples with output and detailed explanation for each example.