W3cubDocs

/Web APIs

TextEncoder

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 TextEncoder interface enables you to encode a JavaScript string using UTF-8.

Constructor

TextEncoder()

Creates and returns a new TextEncoder.

Instance properties

The TextEncoder interface doesn't inherit any properties.

TextEncoder.encoding Read only

Always returns utf-8.

Instance methods

The TextEncoder interface doesn't inherit any methods.

TextEncoder.encode()

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

TextEncoder.encodeInto()

Takes a string to encode and a destination Uint8Array 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 older encode() method.

Examples

>

Encoding to UTF-8

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

<button id="encode">Encode</button>
<button id="reset">Reset</button>
<div id="output"></div>
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

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
TextEncoder 38 79 18 25 10.1 38 18 25 10.3 3.0 38 10.3
TextEncoder 38 79 18 25 10.1 38 18 25 10.3 3.0 38 10.3
encode 38 79 18 25 10.1 38 18 25 10.3 3.0 38 10.3
encodeInto 74 79 66 62 14.1 74 66 50 14.5 11.0 74 14.5
encoding 38 79 18 25 10.1 38 18 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 TextDecoder 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/TextEncoder