TextDecoderStream()

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

Syntax

new TextDecoderStream(label,options);

Parameters

label

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

optionsOptional

A TextDecoderOptions dictionary with the property:

fatal

A boolean value indicating the error mode. If true then a DOMException will be thrown if the decoder encounters an error. Defaults to false.

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.

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

Specifications

Specification
Encoding
# ref-for-dom-textdecoderstream①

Browser compatibility

BCD tables only load in the browser