To encode a string we need encodeURIComponent() or encodeURI() and to decode a string we need decodeURIComponent() or decodeURI(). Initially, we have used escape() to encode a string but since it is deprecated we are now using encodeURI().
syntax-1
encodeURIComponent(string);
syntax-2
decodeURIComponent(string);
Example
In the following example, initially, a string is taken and encoded using encodeURI() and later decoded using decodeURI(). Later on both the encoded and decoded results were displayed in the output.
<html> <body> <p id = "encoding"></p> <script> var str = "Tutorix is the best e-learning platform"; var enc = encodeURI(str); var dec = decodeURI(enc); var res = "After encoding: " + enc + " </br>" + "After Decoding: " + dec; document.getElementById("encoding").innerHTML = res; </script> </body> </html>
Output
After encoding: Tutorix%20is%20the%20best%20e-learning%20platform After Decoding: Tutorix is the best e-learning platform