![]() |
VOOZH | about |
The TextDecoder is a NodeJS interface that can for a specific text encoding, such as UTF-8, ISO-8859-2, KOI8-R, GBK, etc. decode it. It takes a stream of bytes as input and gives as output a stream of code points.
Import:
let var = new TextDecoder(encoding);
Parameter:
Constructor:
Properties:
The TextDecoder does not inherit any property. The defined properties are:
Functions:
Return Value: A string representing the decoded value for the given encoding format.
Example 1: Let us decode UTF-8 type encoded codepoints. As we know by default 'UTF-8' will be used hence we need not define encoding in the parameters of the decoder constructor. Create a file index.js and write the below code:
Steps to run the application: Write the below command in the terminal to start the server:
node index.js
Output:
Hello
Example 2: To Decode non-UTF-8 encoded values, like A Russian Slogan "Привет, мир!" written in English as "Hello, World". We can use the 'windows-1251' encoding type to decode its code points. Create a file index.js and add the following code:
Steps to run the application: Write the below command in the terminal to start the server:
node index.js
Output:
Привет, мир!
Browser Compatibility:
| BROWSER | VERSION SUPPORTED |
| Chrome | 38 |
| Edge | 79 |
| Firefox | 19 |
| Internet Explorer | NOT SUPPORTED |
| Opera | 25 |
| Safari | 10.1 |
| Android WebView | 38 |
| Samsung Internet | 3.0 |
| Deno | 1.0 |
| NodeJS | 11.0.0 |
| Safari iOS | 10.3 |
Reference: https://nodejs.org/api/util.html#new-textdecoderencoding-options