However, they are still widely supported across many browsers for backward compatibility.
Here's a basic usage example of the
let escapedString = escape('Hello, World!'); console.log(escapedString); // 'Hello%2C%20World%21' let unescapedString = unescape(escapedString); console.log(unescapedString); // 'Hello, World!'
In the example above, the string 'Hello, World!' gets encoded using the
escape() function. The space and the exclamation mark become
%21, respectively, in the encoded string. We can then use the
unescape() function to decode this encoded string back to its original form.
Now, let's imagine a scenario where you might want to use the
unescape() function. Suppose you're working on a web project, and you have a URL string that has been previously escaped using
escape(). Now, you want to display the URL in a human-readable form on your webpage. Here's how you could use
let escapedUrl = escape('https://example.com/?q=Hello, World!'); console.log(escapedUrl); // 'https%3A//example.com/%3Fq%3DHello%2C%20World%21' let unescapedUrl = unescape(escapedUrl); console.log(unescapedUrl); // 'https://example.com/?q=Hello, World!'
unescape() function turns the encoded URL back into its original form.
unescape() is useful and easy to use, you should know that the modern approach is to use
decodeURIComponent(). These functions provide more comprehensive support for UTF-8 encoding, which is important for supporting internationalized domain names and URLs.
escape() for encoding. While it is not the modern way of decoding URI components due to its lack of full support for UTF-8 encoding, its simple usage and broad browser support can make it a good choice when backward compatibility is a priority.