dom / latest / textencoder.html /

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

Example

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

Constructor

TextEncoder()

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

Properties

The TextEncoder interface doesn't inherit any property.

TextEncoder.prototype.encoding Read only

Always returns "utf-8".

Methods

The TextEncoder interface doesn't inherit any method.

TextEncoder.encode()

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

TextEncoder.encodeInto()

Takes a USVString to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. This is potentially more performant than the older encode() method.

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–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder