TextEncoder
{{APIRef("Encoding API")}}
{{AvailableInWorkers}}
The TextEncoder
interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
Constructor
{{DOMxRef("TextEncoder.TextEncoder", "TextEncoder()")}}
- : Returns a newly constructed
TextEncoder
that will generate a byte stream with UTF-8 encoding.
- : Returns a newly constructed
Instance properties
The TextEncoder
interface doesn’t inherit any properties.
{{DOMxRef("TextEncoder.encoding")}}
{{ReadOnlyInline}}
- : Always returns
utf-8
.
- : Always returns
Instance methods
The TextEncoder
interface doesn’t inherit any methods.
{{DOMxRef("TextEncoder.encode()")}}
- : Takes a string as input, and returns a
{{jsxref("Uint8Array")}}
containing UTF-8 encoded text.
- : Takes a string as input, and returns a
{{DOMxRef("TextEncoder.encodeInto()")}}
- : Takes a string to encode and a destination
{{jsxref("Uint8Array")}}
to put resulting UTF-8 encoded text into, and returns an object indicating the progress of the encoding. This is potentially more performant than the olderencode()
method.
- : Takes a string to encode and a destination
Examples
const encoder = new TextEncoder();
const view = encoder.encode("€");
console.log(view); // Uint8Array(3) [226, 130, 172]
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- The
{{DOMxRef("TextDecoder")}}
interface describing the inverse operation. - Node.js supports global export from v11.0.0