Baseline Widely available
Note: This feature is available in Web Workers.
The TextEncoder
interface enables you to encode a JavaScript string using UTF-8.
TextEncoder()
Creates and returns a new TextEncoder
.
The TextEncoder
interface doesn't inherit any properties.
TextEncoder.encoding
Read only
Always returns utf-8
.
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.
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 See also
TextDecoder
interface describing the inverse operation.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4