docs.rodeo

MDN Web Docs mirror

TextEncoder

{{APIRef("Encoding API")}} {{AvailableInWorkers}} 

The TextEncoder interface enables you to {{glossary("character encoding", "encode")}}  a JavaScript string using {{glossary("UTF-8")}} .

Constructor

Instance properties

The TextEncoder interface doesn’t inherit any properties.

Instance methods

The TextEncoder interface doesn’t inherit any methods.

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>
div {
  margin: 1rem 0;
}
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();
});

{{embedlivesample("Encoding to UTF-8")}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN