W3cubDocs

/Web APIs

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 "𠮷".

<button id="decode">Decode</button>
<button id="reset">Reset</button>
<div id="output"></div>
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.

<button id="decode">Decode</button>
<button id="reset">Reset</button>
<div id="decoded"></div>
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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
TextDecoder 38 79 19
18–19Implemented a slightly different version of the spec.
25 10.1 38 19
18–19Implemented a slightly different version of the spec.
25 10.3 3.0 38 10.3
TextDecoder 38 79 19
18–19Implemented a slightly different version of the spec.
25 10.1 38 19
18–19Implemented a slightly different version of the spec.
25 10.3 3.0 38 10.3
decode 38 79 19
18–19Implemented a slightly different version of the spec.
25 10.1 38 19
18–19Implemented a slightly different version of the spec.
25 10.3 3.0 38 10.3
encoding 38 79 19
18–19Implemented a slightly different version of the spec.
25 10.1 38 19
18–19Implemented a slightly different version of the spec.
25 10.3 3.0 38 10.3
fatal 38 79 36 25 10.1 38 36 25 10.3 3.0 38 10.3
ignoreBOM 38 79 63 25 10.1 38 63 25 10.3 3.0 38 10.3
worker_support 38 79 20 25 10.1 38 20 25 10.3 3.0 38 10.3

See also

  • The TextEncoder interface describing the inverse operation.

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder