Web API | TextDecoder decode() Method

The decode() method in TextDecoder API is used to takes a stream of bytes as input and emits a stream of code points. The TextEncoder decode() method takes an ArrayBuffer containing the encoded data and options object and returns the original string (i.e. decoded string).

Syntax:

decoder.decode(buffer, options)

Parameters:

  • buffer: It is an ArrayBuffer, Dataview or TypedArray instance that contains the encoded data.
  • options: It is optional parameter that holds an object containing some properties.
    • stream: It is a boolean value. It is set to true if datas are processing in chunks and false if datas are not chunked (by dafault its value set to false)

Return Value: It decodes the encoded input in buffer and returns the decoded string.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        TextEncoder Web API
        encodeInto() method
    </title>
</head>
  
<body>
    <p id='javascript'>I Love javascript</p>
  
    <script type="text/javascript">
  
        // Orginal string
        const orginal = document.querySelector(
                '#javascript').textContent
  
        // Instsnce of TextEncoder
        const encoder = new TextEncoder()
  
        // Instance of TextDecoder
        const decoder = new TextDecoder()
  
  
        // encode() is just an another method
        // defined in TextEncoder class
        // It specifies the encoded result
        // of strings
        const encodedResult = encoder.encode(orginal)
  
        // Decoding
        const decodedResult = decoder.decode(encodedResult)
  
        console.log(`
            Orginal String:  ${orginal},
            Encoded Value:   ${encodedResult},
            Decoded Value:   ${decodedResult}
        `)
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>GFG TextEncoder encodeInto() method</title>
</head>
  
<body>
    <p id='javascript' hidden>I Love javascript</p>
    <button id='btn'>Click me to see results</button>
    <div id='result'></div>
  
    <script type="text/javascript">
  
        // Orginal string
        const orginal = document.querySelector(
                '#javascript').textContent
  
        const btn = document.querySelector('#btn')
        const result = document.querySelector('#result')
  
        // Instsnce of TextEncoder
        const encoder = new TextEncoder()
  
        // Instance of TextDecoder
        const decoder = new TextDecoder()
  
  
        // encode() is just an another method
        // defined in TextEncoder class
        // It specifies the encoded result of strings
        const encodedResult = encoder.encode(orginal)
  
        // Decoding
        const decodedResult = decoder.decode(encodedResult)
  
        // Button listining for click event, when
        // click occurs shows orginal string and 
        // Encoded and Decode values
  
        btn.addEventListener('click', () => {
            result.innerHTML = `
        <p><strong>Orginal: </strong> ${orginal}</p>
        <p><strong>Encoded Value: </strong> ${encodedResult}</p>
        <p><strong>Decoded Value: </strong> ${decodedResult}</p>
      `
        })
    </script>
</body>
  
</html>

chevron_right


Output:
Before Clicking the Button:

After Clicking the Button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.