The TextEncoder.encode() method takes a string as input, and returns a Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
The TextEncoder.encode() method takes a string as input, and returns a Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
js
encode(string)
stringA string containing the text to encode.
A Uint8Array object.
html
<p class="source">This is a sample paragraph.</p> <p class="result">Encoded result:</p>
js
const sourcePara = document.querySelector(".source"); const resultPara = document.querySelector(".result"); const string = sourcePara.textContent; const textEncoder = new TextEncoder(); let encoded = textEncoder.encode(string); resultPara.textContent += encoded;
| Specification |
|---|
| Encoding Standard # ref-for-dom-textencoder-encode① |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
encode |
38 | 79 | 18 | No | 25 | 10.1 | 38 | 38 | 18 | 25 | 10.3 | 3.0 |
TextEncoder interface it belongs to.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encode