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
TextEncoderthat will generate a byte stream with UTF-8 encoding.
Properties
The TextEncoder interface doesn't inherit any property.
-
TextEncoder.prototype.encodingRead only -
Always returns "
utf-8".
Methods
The TextEncoder interface doesn't inherit any method.
TextEncoder.prototype.encode()-
Takes a
USVStringas input, and returns aUint8Arraycontaining UTF-8 encoded text. TextEncoder.prototype.encodeInto()-
Takes a
USVStringto encode and a destinationUint8Arrayto 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 olderencode()method.
Specifications
| Specification |
|---|
| Encoding # interface-textencoder |
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 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
No |
25 |
10.1 |
38 |
38 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
Yes |
10.3 |
3.0 |
TextEncoder |
53
Does not accept parameters. Supports only
utf-8 encoding.38-53
Throws
RangeError exception for unknown encoding types. |
79
Does not accept parameters. Supports only
utf-8 encoding. |
48
The constructor accepts an encoding type label argument, but the value is ignored. Only
utf-8 encoding is supported.38-48
If the encoding type label argument is invalid, then a
RangeError exception is thrown.19-38
If the encoding type label argument is invalid, then a
TypeError exception is thrown.18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
No |
25 |
10.1 |
38 |
38 |
48
The constructor accepts an encoding type label argument, but the value is ignored. Only
utf-8 encoding is supported.38-48
If the encoding type label argument is invalid, then a
RangeError exception is thrown.19-38
If the encoding type label argument is invalid, then a
TypeError exception is thrown.18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
? |
10.3 |
3.0 |
encode |
38 |
79 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
No |
25 |
10.1 |
38 |
38 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
Yes |
10.3 |
3.0 |
encodeInto |
74 |
79 |
66 |
No |
No |
14.1 |
74 |
74 |
66 |
No |
14.5 |
11.0 |
encoding |
38 |
79 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
No |
25 |
10.1 |
38 |
38 |
19
18
Firefox 18 implemented an earlier and slightly different version of the specification.
|
Yes |
10.3 |
3.0 |
worker_support |
38 |
79 |
20 |
No |
25 |
10.1 |
38 |
38 |
20 |
? |
10.3 |
3.0 |
See also
- The
TextDecoderinterface describing the inverse operation. - Node.js supports global export from v11.0.0
© 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