TextDecoder()

The TextDecoder() constructor returns a newly created TextDecoder object for the encoding specified in parameter.

If the value for utfLabel is unknown, or is one of the two values leading to a 'replacement' decoding algorithm ("iso-2022-cn" or "iso-2022-cn-ext"), a RangeError is thrown.

Syntax

new TextDecoder()
new TextDecoder(utfLabel)
new TextDecoder(utfLabel, options)

Parameters

utfLabel Optional

A string, defaulting to "utf-8", containing the label of the encoder. This may be any valid label.

options Optional

A TextDecoderOptions dictionary with the property:

fatal

A Boolean flag indicating if the TextDecoder.decode() method must throw a TypeError when an coding error is found. It defaults to false.

Examples

const textDecoder1 = new TextDecoder("iso-8859-2");
const textDecoder2 = new TextDecoder();
const textDecoder3 = new TextDecoder("csiso2022kr", { fatal: true }); // Allows TypeError exception to be thrown.
const textDecoder4 = new TextDecoder("iso-2022-cn"); // Throw a RangeError exception.

Specifications

Specification
Encoding
# ref-for-dom-textdecoder①

Browser compatibility

BCD tables only load in the browser

See also