JavaScript (ES2026) reference · String Methods

JavaScript String isWellFormed() Method

Return true if the string has no lone surrogates, i.e. it can be safely encoded as UTF-8.

JavaScript (ES2026) Since ES2024

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.isWellFormed()

Return value

true or false.

Examples

Detect broken charactersTry it yourself
console.log("ok \u{1F600}".isWellFormed(), "broken \uD83D".isWellFormed());

Output

true false
Make text safe before encodingTry it yourself
const input = "x\uDC00y";
const safe = input.isWellFormed() ? input : input.toWellFormed();
console.log(encodeURIComponent(safe));

Output

x%EF%BF%BDy

Version history

VersionChange
JavaScript ES2024Added

Was this page helpful?