Open In App

Difference between unescape() and escape() functions in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

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.


Last Updated : 09 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads