Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TextEncoder

BaselineWidely available

Note: This feature is available inWeb Workers.

TheTextEncoder interface enables you toencode a JavaScript string usingUTF-8.

Constructor

TextEncoder()

Creates and returns a newTextEncoder.

Instance properties

TheTextEncoder interface doesn't inherit any properties.

TextEncoder.encodingRead only

Always returnsutf-8.

Instance methods

TheTextEncoder interface doesn't inherit any methods.

TextEncoder.encode()

Takes a string as input, and returns aUint8Array containing the string encoded using UTF-8.

TextEncoder.encodeInto()

Takes a string to encode and a destinationUint8Array to put the resulting UTF-8 encoded text into, and returns an object indicating the progress of the encoding.This is potentially more performant than the olderencode() method.

Examples

Encoding to UTF-8

This example shows how to encode the "€" character to UTF-8.

html
<button>Encode</button><button>Reset</button><div></div>
div {  margin: 1rem 0;}
js
const utf8encoder = new TextEncoder();const text = "€";const output = document.querySelector("#output");const encodeButton = document.querySelector("#encode");encodeButton.addEventListener("click", () => {  output.textContent = utf8encoder.encode(text);});const resetButton = document.querySelector("#reset");resetButton.addEventListener("click", () => {  window.location.reload();});

Specifications

Specification
Encoding
# interface-textencoder

Browser compatibility

See also

  • TheTextDecoder 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