TextDecoder

Baseline Widely available

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

Note: This feature is available in Web Workers.

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.

Constructor

TextDecoder()

Creates and returns a new TextDecoder.

Instance properties

The TextDecoder interface doesn't inherit any properties.

TextDecoder.encoding Read only

A string containing the name of the character encoding system that this TextDecoder will use.

TextDecoder.fatal Read only

A boolean indicating whether the error mode is fatal.

TextDecoder.ignoreBOM Read only

A boolean indicating whether the byte order mark is ignored.

Instance methods

The TextDecoder interface doesn't inherit any methods.

TextDecoder.decode()

Decodes the given bytes into a JavaScript string and returns it.

Examples

Decoding UTF-8 text

This example shows how to decode the UTF-8 encoding of the character "𠮷".

html
<button id="decode">Decode</button>
<button id="reset">Reset</button>
<div id="output"></div>
js
const utf8decoder = new TextDecoder(); // default 'utf-8'
const encodedText = new Uint8Array([240, 160, 174, 183]);

const output = document.querySelector("#output");
const decodeButton = document.querySelector("#decode");
decodeButton.addEventListener("click", () => {
  output.textContent = utf8decoder.decode(encodedText);
});

const resetButton = document.querySelector("#reset");
resetButton.addEventListener("click", () => {
  window.location.reload();
});

Decoding non-UTF8 text

In this example, we decode the Russian text "Привет, мир!", which means "Hello, world." In our TextDecoder() constructor, we specify the Windows-1251 character encoding.

html
<button id="decode">Decode</button>
<button id="reset">Reset</button>
<div id="decoded"></div>
js
const win1251decoder = new TextDecoder("windows-1251");
const encodedText = new Uint8Array([
  207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33,
]);

const decoded = document.querySelector("#decoded");
const decodeButton = document.querySelector("#decode");
decodeButton.addEventListener("click", () => {
  decoded.textContent = win1251decoder.decode(encodedText);
});

const resetButton = document.querySelector("#reset");
resetButton.addEventListener("click", () => {
  window.location.reload();
});

Specifications

Specification
Encoding
# interface-textdecoder

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
TextDecoder
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Partial support
Firefox 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox – Full support
Firefox 19 (Release date: 2013-02-19)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox for Android – Full support
Firefox for Android 19 (Release date: 2013-02-19)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Partial support
Node.js 8.3 – 10.9 (Release date: 2017-08-09)
footnote Partial support
footnote Available as a part of the util module.
Node.js – Full support
Node.js 11 (Release date: 2018-10-23)
footnote Full support
TextDecoder() constructor
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Partial support
Firefox 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox – Full support
Firefox 19 (Release date: 2013-02-19)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox for Android – Full support
Firefox for Android 19 (Release date: 2013-02-19)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8.3 (Release date: 2017-08-09)
footnote Full support
decode
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Partial support
Firefox 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox – Full support
Firefox 19 (Release date: 2013-02-19)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox for Android – Full support
Firefox for Android 19 (Release date: 2013-02-19)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote
footnote Before Deno 1.11, passing option.stream is not supported and results in an error being thrown.
Node.js – Full support
Node.js 8.3 (Release date: 2017-08-09)
footnote Full support
encoding
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Partial support
Firefox 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox – Full support
Firefox 19 (Release date: 2013-02-19)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 18 – 18 (Release date: 2013-01-08)
footnote Partial support
footnote Implemented a slightly different version of the spec.
Firefox for Android – Full support
Firefox for Android 19 (Release date: 2013-02-19)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8.3 (Release date: 2017-08-09)
footnote Full support
fatal
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 36 (Release date: 2015-02-24)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Full support
Firefox for Android 36 (Release date: 2015-02-27)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8.3 (Release date: 2017-08-09)
footnote Full support
ignoreBOM
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 63 (Release date: 2018-10-23)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Full support
Firefox for Android 63 (Release date: 2018-10-23)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1.0.5 (Release date: 2023-10-11)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8.3 (Release date: 2017-08-09)
footnote Full support
Available in workers
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 20 (Release date: 2013-04-02)
footnote Full support
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – Full support
Firefox for Android 20 (Release date: 2013-04-02)
footnote Full support
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10.3 (Release date: 2017-03-27)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 11.7 (Release date: 2019-01-18)
footnote Full support

Legend

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

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

See also

  • The TextEncoder interface describing the inverse operation.