JavaScript > Global Reference

Decode a string in JavaScript

How to use unescape function to decode a string in JavaScript?


The unescape() function is used to decode a string in JavaScript.

NOTE: The unescape() function is outdated function in the JavaScript, instead of unescape() function we are using decodeURI() & decodeURIComponent() in JavaScript.

<p>Click the below button to run the UnEsacpe function.</p>
<input type="button" onclick="myFunction()" value="UnEscape(Decode)">
<p id="myId"></p>

<script>
    function myFunction() {
        var u = "Want to learn technologies? Go through TechFunda.com";
        var v = escape(U);
        var d = unescape(E);
        var r = "Encode Result is:" + E + "</br>" + "Decode Result is:" + D;
        document.getElementById("myId").innerHTML = R;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myFunction() in the<script>block which is connected to the onclick of the HTML button and there is a string with value "Want to lear technologies? Go through TechFunda.com" to the variable u, we need to decode the u variable, for that we are using unescape() function in Variable d (Although at eunscape() function is not used in JavaScript, for expalining the code we are using unescape() function). To decode a string we need the encoded string, for that first we are usnig escape() function to encode the string in varaible e, and then unescape() function to decode the encoded string. Onclick of the button "UnEscape(Decode)" in the HTML code fires the function myFunction() in the <script> block at the same type unscape() function decodes the encoded string value and returns as output.

OUTPUT

 Views: 638 | Post Order: 187



Write for us