Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
JavaScript unescape() with example
The JavaScript unescape() function is used to decode strings that were encoded with the escape() function. This function is deprecated since JavaScript 1.5 and should not be used in modern code. Use decodeURIComponent() instead.
Syntax
unescape(encodedString)
Parameter: encodedString - The string to be decoded.
Return Value: A new decoded string.
Example with unescape() (Deprecated)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript unescape() Example</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.output {
background: #f4f4f4;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
}
</style>
</head>
<body>
<h2>JavaScript unescape() Function</h2>
<div id="encoded" class="output"></div>
<div id="decoded" class="output"></div>
<button onclick="demonstrateUnescape()">Decode String</button>
<script>
function demonstrateUnescape() {
let originalString = "Hello World! Special chars: @#$%";
let encodedString = escape(originalString);
let decodedString = unescape(encodedString);
document.getElementById('encoded').innerHTML =
"<strong>Encoded:</strong> " + encodedString;
document.getElementById('decoded').innerHTML =
"<strong>Decoded:</strong> " + decodedString;
}
// Show initial encoded string
let sample = "Hello World! Special chars: @#$%";
document.getElementById('encoded').innerHTML =
"<strong>Encoded:</strong> " + escape(sample);
</script>
</body>
</html>
Modern Alternative: decodeURIComponent()
Since unescape() is deprecated, use decodeURIComponent() for URL decoding:
// Modern approach using decodeURIComponent()
let encodedURL = "Hello%20World%21%20Special%20chars%3A%20%40%23%24%25";
let decodedURL = decodeURIComponent(encodedURL);
console.log("Encoded:", encodedURL);
console.log("Decoded:", decodedURL);
Encoded: Hello%20World%21%20Special%20chars%3A%20%40%23%24%25 Decoded: Hello World! Special chars: @#$%
Comparison
| Method | Status | Use Case | Recommendation |
|---|---|---|---|
unescape() |
Deprecated | Legacy string decoding | Avoid in new code |
decodeURIComponent() |
Standard | URL component decoding | Use this instead |
Key Points
-
unescape()is deprecated and should not be used in modern JavaScript - It decodes strings encoded with the deprecated
escape()function - Use
decodeURIComponent()for URL decoding in modern applications - The function may be removed from browsers in the future
Conclusion
While unescape() can decode escaped strings, it's deprecated and should be avoided. Use decodeURIComponent() for modern URL decoding needs.
Advertisements
