Skip to content

utf8Encode ​

Encodes a string into UTF-8, returning a Uint8Array.

Syntax ​

typescript
utf8Encode(input: string): Uint8Array

Parameters ​

NameTypeDescription
inputstringThe string to be encoded into UTF-8

Returns ​

TypeDescription
Uint8ArrayThe bytes of the UTF-8 encoded string

Example ​

typescript
utf8Encode("Olá"); // Uint8Array([...])
utf8Encode(123 as any); // throws TypeError

Notes ​

  • Supports environments with TextEncoder (modern browsers) and Buffer (Node.js).
  • Throws an error if no UTF-8 encoder is available.

References ​

Released under the MIT License.