TextEncoder
The TextEncoder
interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
Note: This feature is available in Web Workers
Example
const encoder = new TextEncoder()
const view = encoder.encode('€')
console.log(view); // Uint8Array(3) [226, 130, 172]
Constructor
TextEncoder()
-
Returns a newly constructed
TextEncoder
that will generate a byte stream with UTF-8 encoding.
Properties
The TextEncoder
interface doesn't inherit any property.
TextEncoder.prototype.encoding
Read only-
Always returns "
utf-8
".
Methods
The TextEncoder
interface doesn't inherit any method.
TextEncoder.prototype.encode()
-
Takes a
USVString
as input, and returns aUint8Array
containing UTF-8 encoded text. TextEncoder.prototype.encodeInto()
-
Takes a
USVString
to encode and a destinationUint8Array
to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. This is potentially more performant than the olderencode()
method.
Specifications
Specification |
---|
Encoding # interface-textencoder |
Browser compatibility
BCD tables only load in the browser
See also
- The
TextDecoder
interface describing the inverse operation. - Node.js supports global export from v11.0.0