Difference between unescape() and escape() functions in JavaScript
Last Updated :
09 Jan, 2023
In this article, we will learn about the escape() & unescape() functions in JavaScript. We will understand the purpose of using both these functions through the example. Later in this article, we will discuss the difference between escape() & unescape() functions. Let's discuss the escape() function.
JavaScript escape() function: This function takes a string as a single parameter & encodes the string that can be transmitted over the computer network which supports ASCII characters. Encoding is the process of converting plain text into ciphertext.
Syntax:
escape( string )
Note: The escape() function only encodes the special characters, this function is deprecated.
Exceptions: @ – + . / * _
Example: In this example, we have used the special character to see the changes.
JavaScript
// Special character encoded with escape function
console.log(escape("Geeks for Geeks!!!"));
// Print encoded string using escape() function
// Also include exceptions i.e. @ and .
console.log(escape("To contribute articles contact"+
" us at contribute@geeksforgeeks.org"));
Output:
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org
From the above output, we can see the exception in the email address with the special symbol "@" is not encoded & displays the same as in the input & the rest of the text got encoded.
Now, if we want to convert the encoded text ie., ciphertext to normal readable text then we have to use unescape() function that will decode the encoded text. Decoding is the process of converting ciphertext into plain text.
JavaScript unescape() function: This function takes a string as a single parameter and uses it to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape() function.
Syntax:
unescape(string)
Note: This function only decodes the special characters, this function is deprecated.
Exceptions: @ – + . / * _
Example 1: In this example, we have used the special character to see the changes.
JavaScript
// Special character encoded with escape function
console.log(unescape("Geeks%20for%20Geeks%21%21%21"));
// Print encoded string using escape() function
// Also include exceptions i.e. @ and .
console.log(unescape("To%20contribute%20articles%20contact"+
"%20us%20atcontribute@geeksforgeeks.org"));
Output:
Geeks for Geeks!!!
To contribute articles contact us at
contribute@geeksforgeeks.org
From the above example, we can see the ciphertext gets decode to the plain text by using the unescape() function.
Example 2:
JavaScript
// Special character encoded with escape function
var str = escape("Geeks for Geeks!!!");
console.log("Encoded : " + str);
// unescape() function
console.log("Decoded : " + unescape(str))
// The exception
// @ and . not encoded.
str = escape("To contribute articles contact us" +
"at contribute@geeksforgeeks.org")
console.log("Encoded : " + str);
// unescape() function
console.log("Decoded : " + unescape(str))
Output:
Encoded : Geeks%20for%20Geeks%21%21%21
Decoded : Geeks for Geeks!!!
Encoded : To%20contribute%20articles%20contact%20us%20
at%20contribute@geeksforgeeks.org
Decoded : To contribute articles contact us at
contribute@geeksforgeeks.org
Difference between unescape() & escape() function:
unescape() | escape() |
---|
The unescape() function is used to decode that string encoded by the escape() function.
| The escape() function in JavaScript is used for encoding a string. Using ASCII character support, it makes a string portable so that it can be transmitted across any network to any computer.
|
A decoded string is returned.
| An encoded string is returned.
|
This function only encodes the special characters, this function is deprecated.
It has certain Exceptions: @ – + . / * _
| This function only encodes the special characters, this function is deprecated.
It has certain Exceptions: @ – + . / * _
|
It takes the parameter as a string containing characters in the form "%xx", where xx is a 2-digit hexadecimal number. | It takes the parameter as a string in the ISO-Latin-1 character set. |
The unescape function is a property of the global object | The escape function is also a property of the global object. |
Similar Reads
Difference between Anonymous and Named functions in JavaScript
In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k
4 min read
Understanding the Difference between Pure and Impure Functions in JavaScript
In this article, we will see the concepts of pure and impure functions in JavaScript, along with understanding their differences, & basic implementation for a better understanding of the concepts. Pure Functions: This function always returns the same output as given the same input parameters. Pu
5 min read
What is the difference between freeze and seal in JavaScript?
In JavaScript, Object.freeze makes an object immutable, preventing any changes to existing properties and values. Object.seal allows changes to existing properties but prevents adding or removing properties. Both methods enforce strict immutability, but freeze is stricter than seal. Table of Content
2 min read
Difference between function expression vs declaration in JavaScript
Function Declaration: A Function Declaration( or a Function Statement) defines a function with the specified parameters without requiring a variable assignment. They exist on their own, i.e, they are standalone constructs and cannot be nested within a non-function block. A function is declared using
1 min read
Difference between != and !== operator in JavaScript
!= operatorThe inequality operator (!=) is the logical opposite of the equality operator. It means "Not Equal" and returns true whereas equality would return false and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing. For example 1 !=
2 min read
Difference Between encodeURI and encodeURIComponent in JavaScript
The encodeURI and encodeURIComponent functions are used to encode a URI by transforming characters that could otherwise cause issues when included in a URI. While both functions are used for encoding, they serve different purposes and encode different sets of characters. Understanding the difference
2 min read
Difference between Type Error and Reference Error in JavaScript
JavaScript is one of the most popular programming languages in the world, and it is used by millions of developers to create dynamic and interactive web applications. However, like any programming language, JavaScript is not without its quirks and pitfalls. Two common issues that developers often en
6 min read
Difference between âfunction declarationâ and âfunction expression' in JavaScript
Functions in JavaScript allow us to carry out some set of actions, important decisions, or calculations and even make our website more interactive. In this article, we will learn the difference between âfunction declarationâ and âfunction expressionâ. The similarity is both use the keyword function
2 min read
What is the difference between Map and WeakMap in JavaScript ?
In this article, we will talk about the difference between Map and WeakMap which are introduced by ES6. Javascript object supports only one key object. For supporting multiple key objects, Then Map comes on this path. Map: A Map is an unordered list of key-value pairs where the key and the value can
4 min read
Difference Between == & === in JavaScript
In JavaScript, equality operators are used to compare values. However, == (double equals) and === (triple equals) behave differently: == (Loose Equality): Converts values to the same data type before comparing (type coercion).=== (Strict Equality): Compares both value and data type, without converti
2 min read