TextEncoder and TextDecoder using JavaScript

JavaScript TextEncoder and TextDecoder are the functions to encode and decode the given value, respectively. The TextEncoder works to convert a specified string into the utf-8 standard using the JavaScript function. It transforms the string into a Uint8Array. Using a JavaScript function, the TextDecoder converts specific input bytes into a required code point. It can decode ISO-8859-2, KOI8-R, UTF-8, GBK, and other file types.

TextDecoder using JavaScript

The function can read the value into a JavaScript string using the built-in TextDecoder object.

Syntax

Firstly, we can use the following syntax to decode the input value.

  • input_label: it typically encodes the value; by default, it is utf-8. There is also support for big5, windows-1251, and many other encodings.
  • Options: it is an Optional object in the decoder method.
  • Fatal is a boolean value. If the value shows the truth, it causes invalid (non-decodable) characters to be excluded by default. If value shows the false, they are replaced with the character \uFFFD.
  • The ignoreBOM is a boolean and is rarely used. If Boolean shows true, then ignores the optional byte-order Unicode mark (BOM).

Decoding comes next, and it works like this:

  • The "input_value" is essential input with the function to decode the buffer source.
  • The "options" is the optional object with the decode method.
  • When the decoder is constantly called with input data, the stream is true for decoding streams.

Examples

The following examples show the different types of data to decode the data.

Example1

The following example shows the given objects of the input label. We can see the decoded data of the input stream or string. Here, the example uses, by default, "Uint8Array" encoded values.

Output

The following image shows the decoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Example2

The following example shows the given objects of the input label. We can see the decoded data of the input stream or string. We can use the onclick button with the event to display data. We can see the upper-case and lower-case encoded data with other encoded information.

Output

The following image shows the decoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Example3

The following example shows TextDecoder with the sub-array using objects of the input label. We can see the decoded data of the input stream or string with the given array length. We can use the onclick button with the event to display data.

Output

The following image shows the decoding data of the input value.

TextEncoder and TextDecoder using JavaScript

TextEncoder using JavaScript

The function can read the value into a JavaScript string using the built-in TextDecoder object.

Syntax

Firstly, we can use the following syntax to decode the input value.

  • It only accepts the "utf-8" encoding.
  • It uses two different methods:
  • The encode(str) gives an Uint8Array From a text.
  • encodeInto(str, destination): it encodes string into the specified Uint8Array target.

Secondly, the encode function is used after the TextEncoder method.

  • Encoding converts into streams when the encoder is constantly called with input data.

Examples

The following examples show the different types of data to encode the data.

Example1

The following example shows the given objects of the input label encoded in the required format. We can see the encoded data of the input object from the string. Here, the example uses, by default, "Uint8Array" encoded values.

Output

The following image shows encoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Example2

The following example shows the given objects of the input label. We can see the decoded data of the input stream or string. We can use the onclick button with the event to display data. We can see the upper-case and lower-case encoded data with other encoded information.

Output

The following image shows encoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Example3

The following example shows the given objects of the input label. We can see the decoded data of the input stream or string. We can use the onclick button with the event to display data. We can see the substring of the entire string.

Output

The following image shows encoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Examples of TextEncoder and TextDecoder

The following example shows the combination of the TextEncoder and TextDecoder in JavaScript.

Output

The following image shows encoding data of the input value.

TextEncoder and TextDecoder using JavaScript

Conclusion

The TextEncoder and TextDecoder in JavaScript help to encode and decode the value. It helps to secure and private the data.