Text encoding and decoding
The TextDecoder interface represents a decoder for a specific text encoding, such as UTF-8, ISO-8859-2, or GBK. A decoder takes an array of bytes as input and returns a JavaScript string.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 38 | 79 | 19 | 10.1 | 38 | 10.3 | |
| The TextDecoder.decode() method returns a string containing text decoded from the buffer passed as a parameter. | 38 | 79 | 19 | 10.1 | 38 | 10.3 |
| The TextDecoder.encoding read-only property returns a string containing the name of the character encoding that this decoder will use. | 38 | 79 | 19 | 10.1 | 38 | 10.3 |
| The fatal read-only property of the TextDecoder interface is a Boolean indicating whether the error mode is fatal. | 38 | 79 | 36 | 10.1 | 38 | 10.3 |
| The ignoreBOM read-only property of the TextDecoder interface is a Boolean indicating whether the byte order mark will be included in the output or skipped over. The value of the property is set in the ignoreBOM argument to the decoder's constructor. | 38 | 79 | 63 | 10.1 | 38 | 10.3 |
| The TextDecoder() constructor returns a new TextDecoder object. | 38 | 79 | 19 | 10.1 | 38 | 10.3 |
worker_support Available in workers | 38 | 79 | 20 | 10.1 | 38 | 10.3 |
| The TextDecoderStream interface of the Encoding API converts a stream of text in a binary encoding, such as UTF-8 etc., to a stream of strings. It is the streaming equivalent of TextDecoder. It implements the same shape as a TransformStream, allowing it to be used in ReadableStream.pipeThrough() and similar methods. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The encoding read-only property of the TextDecoderStream interface returns a string containing the name of the encoding algorithm used by the specific decoder. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The fatal read-only property of the TextDecoderStream interface is a Boolean indicating if the error mode of the TextDecoderStream object is set to fatal. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The ignoreBOM read-only property of the TextDecoderStream interface is a Boolean indicating whether the byte order mark will be included in the output or skipped over. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The readable read-only property of the TextDecoderStream interface returns a ReadableStream that emits decoded strings. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The TextDecoderStream() constructor creates a new TextDecoderStream object which is used to convert a stream of text in a binary encoding into strings. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The writable read-only property of the TextDecoderStream interface returns a WritableStream that accepts binary data, in the form of ArrayBuffer, TypedArray, or DataView chunks (SharedArrayBuffer and its views are also allowed), to be decoded into strings. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The TextEncoder interface enables you to character encoding a JavaScript string using UTF-8. | 38 | 79 | 18 | 10.1 | 38 | 10.3 |
| The TextEncoder.encode() method takes a string as input, and returns a Global_Objects/Uint8Array containing the string character encoding using UTF-8. | 38 | 79 | 18 | 10.1 | 38 | 10.3 |
| The TextEncoder.encodeInto() method 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 TextEncoder.encode() method — especially when the target buffer is a view into a Wasm heap. | 74 | 79 | 66 | 14.1 | 74 | 14.5 |
| The TextEncoder.encoding read-only property returns a string containing the name of the encoding algorithm used by the specific encoder. | 38 | 79 | 18 | 10.1 | 38 | 10.3 |
| The TextEncoder() constructor returns a newly created TextEncoder object. | 38 | 79 | 18 | 10.1 | 38 | 10.3 |
worker_support Available in workers | 38 | 79 | 20 | 10.1 | 38 | 10.3 |
| The TextEncoderStream interface of the Encoding API converts a stream of strings into bytes in the UTF-8 encoding. It is the streaming equivalent of TextEncoder. It implements the same shape as a TransformStream, allowing it to be used in ReadableStream.pipeThrough() and similar methods. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The encoding read-only property of the TextEncoderStream interface returns a string containing the name of the encoding algorithm used by the current TextEncoderStream object. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The readable read-only property of the TextEncoderStream interface returns a ReadableStream that emits encoded binary data as Uint8Array chunks. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The TextEncoderStream() constructor creates a new TextEncoderStream object which is used to convert a stream of strings into bytes using UTF-8 encoding. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
| The writable read-only property of the TextEncoderStream interface returns a WritableStream that accepts strings to be encoded into binary data. | 71 | 79 | 105 | 14.1 | 71 | 14.5 |
- This browser only partially implements this feature
- This feature was removed in a later browser version (19)
- Implemented a slightly different version of the spec.
- This browser only partially implements this feature
- This feature was removed in a later browser version (19)
- Implemented a slightly different version of the spec.
- This browser only partially implements this feature
- This feature was removed in a later browser version (19)
- Implemented a slightly different version of the spec.
- This browser only partially implements this feature
- This feature was removed in a later browser version (19)
- Implemented a slightly different version of the spec.
Use cases
-
Using Text encoding and decoding
The TextDecoder interface represents a decoder for a specific text encoding, such as UTF-8, ISO-8859-2, or GBK.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.