The TextEncoder.prototype.encode()
method takes a USVString
as input, and returns a Uint8Array
containing the text given in parameters encoded with the specific method for that TextEncoder
object.
Syntax
b1 = encoder.encode(string);
Parameters
string
- Is a
USVString
containing the text to encode.
Return value
A Uint8Array
object.
Examples
<p class="source">This is a 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();
let encoded = textEncoder.encode(string);
resultPara.textContent += encoded;
Specifications
Specification | Status | Comment |
EncodingThe definition of 'TextEncoder.prototype.encode()' in that specification. | Living Standard | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Chrome
Full support 38 |
Edge
Full support 79 |
Firefox Full support 19 Full support 19 Full support 18 Notes' Firefox 18 implemented an earlier and slightly different version of the specification. |
IE
No support No |
Opera
Full support 25 |
Safari
Full support 10.1 |
WebView Android
Full support 38 |
Chrome Android
Full support 38 |
Firefox Android Full support 19 Full support 19 Full support 18 Notes' Firefox 18 implemented an earlier and slightly different version of the specification. |
Opera Android
Full support Yes |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 3.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.'
- Experimental. Expect behavior to change in the future.
- See implementation notes.'
- See implementation notes.
See also
- The
TextEncoder
interface it belongs to.
TextEncoder.prototype.encode() by Mozilla Contributors is licensed under CC-BY-SA 2.5.