Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TextDecoder

BaselineWidely available

Note: This feature is available inWeb Workers.

TheTextDecoder interface represents a decoder for a specific text encoding, such asUTF-8,ISO-8859-2, orGBK. A decoder takes an array of bytes as input and returns a JavaScript string.

Constructor

TextDecoder()

Creates and returns a newTextDecoder.

Instance properties

TheTextDecoder interface doesn't inherit any properties.

TextDecoder.encodingRead only

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

TextDecoder.fatalRead only

A boolean indicating whether the error mode is fatal.

TextDecoder.ignoreBOMRead only

A boolean indicating whether thebyte order mark is ignored.

Instance methods

TheTextDecoder 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>Decode</button><button>Reset</button><div></div>
div {  margin: 1rem 0;}
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 ourTextDecoder() constructor, we specify the Windows-1251 character encoding.

html
<button>Decode</button><button>Reset</button><div></div>
div {  margin: 1rem 0;}
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

See also

  • TheTextEncoder interface describing the inverse operation.

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp