TextEncoder.prototype.encode()

The TextEncoder.prototype.encode() method takes a USVString as input, and returns a Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.

Syntax

b1 = encoder.encode(string);

Parameters

string

Is a USVString containing the text to encode.

Return value

A Uint8Array object.

Examples

<p class="source">This is a sample paragraph.</p>
<p class="result">Encoded result: </p>
const sourcePara = document.querySelector('.source');
const resultPara = document.querySelector('.result');
const string = sourcePara.textContent;

const textEncoder = new TextEncoder();

let encoded = textEncoder.encode(string);
resultPara.textContent += encoded;

Specifications

Specification
Encoding
# ref-for-dom-textencoder-encode①

Browser compatibility

BCD tables only load in the browser

See also