TextEncoder class

An implementation of the WHATWG Encoding Standard TextEncoder API. All instances of TextEncoder only support UTF-8 encoding.

const encoder = new TextEncoder();
const uint8array = encoder.encode('this is some data');

The TextEncoder class is also available on the global object.

Properties

encoding

The encoding supported by the TextEncoder instance. Always set to 'utf-8'.

Methods

encode(string)

UTF-8 encodes the input string and returns a Uint8Array containing the encoded bytes.

encodeInto(string, Uint8Array)

UTF-8 encodes the src string to the dest Uint8Array and returns an object containing the read Unicode code units and written UTF-8 bytes.

const encoder = new TextEncoder();
const src = 'this is some data';
const dest = new Uint8Array(10);
const { read, written } = encoder.encodeInto(src, dest);

Property Details

encoding

The encoding supported by the TextEncoder instance. Always set to 'utf-8'.

encoding: string

Property Value

string

Method Details

encode(string)

UTF-8 encodes the input string and returns a Uint8Array containing the encoded bytes.

function encode(input?: string): Uint8Array

Parameters

input

string

Returns

Uint8Array

encodeInto(string, Uint8Array)

UTF-8 encodes the src string to the dest Uint8Array and returns an object containing the read Unicode code units and written UTF-8 bytes.

const encoder = new TextEncoder();
const src = 'this is some data';
const dest = new Uint8Array(10);
const { read, written } = encoder.encodeInto(src, dest);
function encodeInto(src: string, dest: Uint8Array): EncodeIntoResult

Parameters

src

string

The text to encode.

dest

Uint8Array

The array to hold the encode result.

Returns