जावास्क्रिप्ट ने स्ट्रिंग को एन्कोड करने के लिए escape() फंक्शन प्रदान किया है। लेकिन चूंकि escape() फंक्शन अब डिप्रिकेटेड है, encodeURI() या encodeURIComponent() का उपयोग करना बेहतर है।
सिंटैक्स-1
escape(string);
सिंटैक्स-2
encodeURIComponent(str);
उदाहरण-1
निम्नलिखित उदाहरण में, escape() मेथड का उपयोग करके स्ट्रिंग "Tutorix is the best e-learning platform!!!" को एन्कोड किया गया है और परिणाम आउटपुट में दिखाया गया है।
<html> <body> <script> document.write(escape("Tutorix is the best e-learning platform!!!")); </script> </body> </html>
आउटपुट
Tutorix%20is%20the%20best%20e-learning%20platform%21%21%21
उदाहरण-2
निम्नलिखित उदाहरण में, encodeURIComponent() मेथड का उपयोग करके प्रदान की गई स्ट्रिंग को एन्कोड किया गया है और परिणाम आउटपुट में दिखाया गया है।
<html> <body> <script> document.write(encodeURIComponent("Tutorix is the best e-learning platform!!!")); </script> </body> </html>
आउटपुट
Tutorix%20is%20the%20best%20e-learning%20platform!!!