CKTIS / JavaScript
TextEncoder — encoding text as UTF-8
TextEncoder converts text into UTF-8 bytes. Learn how encode() differs from encodeInto() and what the read and written counters mean.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
new TextEncoder()
encoder.encoding
encoder.encode(input = "")
encoder.encodeInto(source, destination)TextEncoder is an Encoding Standard API, not part of ECMAScript itself. It supports only UTF-8: its constructor accepts no encoding label, and encoding returns "utf-8".
encode(input) creates a new Uint8Array; omitting input means empty text. encodeInto(source, destination) writes into an existing Uint8Array. It returns read in UTF-16 code units and written in bytes. When space runs out, it does not write a partial UTF-8 sequence.
In the example, the buffer fits Aą, but not the emoji. Check API availability in the target browser; the repository environment does not prove browser support.
Examples
const encoder = new TextEncoder();
const text = "Aą😀";
const bytes = encoder.encode(text);
console.log(encoder.encoding);
console.log(Array.from(bytes).join(","));
const destination = new Uint8Array(4);
const { read, written } = encoder.encodeInto(text, destination);
console.log(`${read} ${written}`);
console.log(Array.from(destination.subarray(0, written)).join(","));Result
utf-8 65,196,133,240,159,152,128 2 3 65,196,133
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.