1 of 20
2 of 20
3 of 20
4 of 20
5 of 20
6 of 20
7 of 20
8 of 20
9 of 20
10 of 20
11 of 20
12 of 20
13 of 20
14 of 20
15 of 20
16 of 20
17 of 20
18 of 20
19 of 20
20 of 20

site

doc

blog

success stories

Encoding

TextEncoder() constructor

TextEncoder() returns a built TextEncoder that generates a UTF-8-encoded data transmission.

Syntax

let encoder = new TextEncoder();

encode() method

The encode() method codes a string object.

Syntax

b1 = encoder.encode(string);

Properties

string A USVString containing the text to be coded.

TextDecoder() constructor

TextDecoder() returns a TextDecoder object that generates a code-point data transmission.

Syntax

let decoder = new TextDecoder(utfLabel, options);

decode() method

decode() method decodes an object using a previously created method in TextDecoder().

Sintaxe

b1 = decoder.decode(buffer, options);
b2 = decoder.decode(buffer);
b3 = decoder.decode();

Properties

buffer Optional

Either an ArrayBuffer or ArrayBufferView containing the text to be decoded.

options Optional

It is a TextDecodeOptions dictionary with the property:

  • stream: boolean indicating that each additional data will follow in subsequent calls to decode(). Set to true if processing the data in lumps, and false for the final chunk or if the data is not blocked. Standard configuration is false.

    Exemple

    addEventListener("fetch", (event) => {
        event.respondWith(handleRequest(event.request, event.console))
    })
    async function handleRequest(request, console_from_event) {
        let utf8decoder = new TextDecoder()
        let u8arr = new Uint8Array([240, 160, 174, 183]);
        let decoded_str = utf8decoder.decode(u8arr)
        console_from_event.log(decoded_str)
        return new Response(decoded_str)
    }
    

For more information on encode and decode, visit MDN Web Docs.


Didn’t find what you were looking for? Open a support ticket.