TextDecoderStream: TextDecoderStream() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2022.

Note: This feature is available in Web Workers.

The TextDecoderStream() constructor creates a new TextDecoderStream object which is used to convert a stream of text in a binary encoding into strings.

Syntax

js
new TextDecoderStream(label)
new TextDecoderStream(label, options)

Parameters

label

A string defaulting to utf-8. This may be any valid label.

options Optional

An object with the following properties:

fatal Optional

A boolean value indicating if the TextDecoder.decode() method must throw a TypeError when decoding invalid data. It defaults to false, which means that the decoder will substitute malformed data with a replacement character.

ignoreBOM Optional

A boolean value indicating whether the byte order mark will be included in the output or skipped over. It defaults to false, which means that the byte order mark will be skipped over when decoding and will not be included in the decoded text.

Exceptions

RangeError

Thrown if the value of label is unknown, or is one of the values leading to a 'replacement' decoding algorithm ("iso-2022-cn" or "iso-2022-cn-ext").

Examples

The following example demonstrates how to decode binary data retrieved from a fetch() call. The data will be interpreted as UTF-8, as no label has been passed.

js
const response = await fetch("https://example.com");
const stream = response.body.pipeThrough(new TextDecoderStream());

Specifications

Specification
Encoding
# ref-for-dom-textdecoderstream①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
TextDecoderStream() constructor
Chrome – Full support
Chrome 71 (Release date: 2018-12-04)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 105 (Release date: 2022-09-20)
footnote Full support
Opera – Full support
Opera 58 (Release date: 2019-01-23)
footnote Full support
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Full support
Chrome Android 71 (Release date: 2018-12-04)
footnote Full support
Firefox for Android – Full support
Firefox for Android 105 (Release date: 2022-09-20)
footnote Full support
Opera Android – Full support
Opera Android 50 (Release date: 2019-02-18)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Full support
Samsung Internet 10 (Release date: 2019-08-22)
footnote Full support
WebView Android – Full support
WebView Android 71 (Release date: 2018-12-04)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Bun – Full support
Bun 1.1.23 (Release date: 2024-08-14)
footnote Full support
Deno – Full support
Deno 1.11 (Release date: 2021-06-08)
footnote Full support
Node.js – Full support
Node.js 16.6 (Release date: 2021-07-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support