Skip to main content
Version: Next

TextEncoder

Encode string text into bytes, it only supports "utf-8" encoding.

See

TextEncoder

Constructors

Constructor

new TextEncoder(): TextEncoder;

Returns

TextEncoder

Example

const encoder = new TextEncoder();

Accessors

encoding

Get Signature

get encoding(): string;

The encoding used by encoder, this always returns "utf-8".

Returns

string

Methods

encode()

encode(input): Uint8Array;

Encode string text to Uint8Array.

Parameters

ParameterTypeDescription

input

string

Text that need encode.

Returns

Uint8Array

Encoded uint8 bytes array.

Example

const encodedBytes = new TextEncoder().encode("Hello, World!");

Throws

Throws TypeError if input is not a string.


encodeInto()

encodeInto(src, dest): object;

Encode string text into Uint8Array.

Parameters

ParameterTypeDescription

src

string

Text that need encode.

dest

Uint8Array

Destination that receives the encoded uint8 bytes array.

Returns

object

Encode result, it contains two numbers: the "read" Unicode code units from src string, and the "written" UTF-8 bytes into the dest buffer.

NameType

read

number

written

number

Throws

Throws TypeError if src is not a string, or dest is not a Uint8Array.