TextDecoder()

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

Syntax

new TextDecoder()
new TextDecoder(label)
new TextDecoder(label, options)

Parameters

label Optional

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

options Optional

An object with the property:

fatal

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

A boolean value indicating whether the byte order mark is ignored. It defaults to false.

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

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