In HTML there is a TextDecoder Interface of which we can create a TextDecoder object for the encoding specified in parameter.
Syntax:
decoder = new TextDecoder( utf-Label, option );
Parameters: This constructor accepts two parameters which are mentioned above and described below:
- utf-Label: Label of the encoder in string format. It is “utf-8” as default.
- option: TextDecoderOptions dictionary which has a property fatal (It is a Boolean flag which indicates if the TextDecoder.decode() method should throw DOMException. It’s default value is false.)
Example 1: This example creating a TextDecoder object with “iso-8859-2” as parameter.
<!DOCTYPE html> <html> <head> <title> TextDecoder Web API | TextDecoder constructor </title> </head> <body> <center> <h1 style="color:green;"> GeeksforGeeks </h1> <h2>TextDecoder constructor</h2> <button onclick="gettextDecoder ();"> Get textDecoder object </button> <p id='textDecoder'></p> <script type="text/javascript"> function gettextDecoder() { var textDecoder1 = new TextDecoder("iso-8859-2"); console.log(textDecoder1); } </script> </center> </body> </html> |
Output:
- Before Click the button:

- After Click the button:

Example: Create a comment object with nothing as parameter. So the default parameter which is “utf-8” will be taken.
<!DOCTYPE html> <html> <head> <title> TextDecoder Web API | TextDecoder constructor </title> </head> <body> <center> <h1 style="color:green;"> GeeksforGeeks </h1> <h2>TextDecoder constructor</h2> <button onclick="gettextDecoder ();"> Get textDecoder object </button> <p id='textDecoder'></p> <script type="text/javascript"> function gettextDecoder() { var textDecoder1 = new TextDecoder(); console.log(textDecoder1); } </script> </center> </body> </html> |
Output:
- Before Click the button:

- After Click the button:

Supported Browsers: The browsers supported by TextDecoder constructor are listed below:
- Google Chrome 38
- Firefox 19
- Opera 25
- Safari 10.1
Recommended Posts:
- Web API | TextDecoder decode() Method
- Web DOMRect API | DOMRect() Constructor
- Differences between Web Services and Web API
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between REST API and SOAP API
- Web Window API | Window locationbar property
- Web API | DOMRect height property
- Web Audio API | AudioContext outputLatency property
- Web ImageData API | ImageData.height property
- Text to Voice conversion using Web Speech API of Google Chrome
- Web TouchEvent API | TouchEvent.changedTouches
- Web Vibration API | Navigator.vibrate() Method
- Web Audio API | AudioNode numberOfOutputs property
- Web Audio API | AudioNode numberOfInputs Property
- Web Audio API | AudioNode context property
- Web Window API | Window scrollbars property
- Web HTMLStyleElement API | StyleElement type property
- Web StyleElement API | StyleElement media property
- Web HTMLElement API | Element accessKeyLabel property
- Web HTMLCanvasElement API | Canvas Element width property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh


