GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — TextEncoder - Web APIs | MDN

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.

  • See full compatibility
  • Learn more

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.

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

  • The TextDecoder interface describing the inverse operation.

Help improve MDN

Learn how to contribute

This page was last modified on Jun 28, 2025 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.