assertIsWellFormedUtf8String

function assertIsWellFormedUtf8String(value): void;

Asserts that a given string can be encoded as UTF-8 without loss.

JavaScript strings are sequences of UTF-16 code units and may contain lone surrogates, which have no UTF-8 representation. TextEncoder silently replaces them with the replacement character (U+FFFD); this function throws instead.

Parameters

ParameterTypeDescription
valuestringThe string to validate.

Returns

void

Throws

If value contains a lone surrogate.

Example

assertIsWellFormedUtf8String('hello 語'); // Passes
assertIsWellFormedUtf8String('\ud800');   // Throws error

On this page