W3cubDocs

/Web APIs

TextEncoder

The TextEncoder interface takes a stream of code points as input and emits a stream of UTF-8 bytes.

Note: This feature is available in Web Workers

Constructor

TextEncoder()

Returns a newly constructed TextEncoder that will generate a byte stream with UTF-8 encoding.

Instance properties

The TextEncoder interface doesn't inherit any properties.

TextEncoder.prototype.encoding Read only

Always returns utf-8.

Instance methods

The TextEncoder interface doesn't inherit any methods.

TextEncoder.encode()

Takes a string as input, and returns a Uint8Array containing UTF-8 encoded text.

TextEncoder.encodeInto()

Takes a string to encode and a destination 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 older encode() method.

Examples

js

const encoder = new TextEncoder();
const view = encoder.encode("€");
console.log(view); // Uint8Array(3) [226, 130, 172]

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
TextEncoder 38 79 18 No 25 10.1 38 38 18 25 10.3 3.0
TextEncoder 38 79 18 No 25 10.1 38 38 18 25 10.3 3.0
encode 38 79 18 No 25 10.1 38 38 18 25 10.3 3.0
encodeInto 74 79 66 No 62 14.1 74 74 66 50 14.5 11.0
encoding 38 79 18 No 25 10.1 38 38 18 25 10.3 3.0
worker_support 38 79 20 No 25 10.1 38 38 20 25 10.3 3.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder