web api instance method
TextEncoder: encode() method
Available in workers
The TextEncoder.encode() method takes a string as input, and returns a Uint8Array containing the string encoded using UTF-8.
Syntax
encode(string)
Parameters
string- : A string containing the text to encode.
Return value
A Uint8Array object containing the UTF-8 encoding of the input string.
Examples
<p class="source">Sample paragraph.</p>
<p class="result">Encoded result:</p>
const sourcePara = document.querySelector(".source");
const resultPara = document.querySelector(".result");
const string = sourcePara.textContent;
const textEncoder = new TextEncoder();
const encoded = textEncoder.encode(string);
resultPara.textContent = `${resultPara.textContent} ${encoded}`;
Interactive exampleOpen the canonical MDN source to run this embedded demo.
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.
See also
- The
TextEncoderinterface it belongs to.