Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 15 Aug, 2021

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.

1. 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

escape( string )

Parameters: This function accepts a single parameter:



  • string: This parameter holds the string that will be encoded.

Return value: Returns an encoded 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




<script>
   // Special character encoded with escape function
   document.write(escape("Geeks for Geeks!!!"));
     
   document.write("<br>");
     
   // Print encoded string using escape() function
   // Also include exceptions i.e. @ and .
   document.write(escape("To contribute articles contact"+
                       " us at contribute@geeksforgeeks.org"));
</script>
  
                 

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. 



2. 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)

Parameters: This function accepts a single parameter:

  • string: This parameter holds the string that will be decoded.

Return value: Returns a decoded 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




<script>
   // Special character encoded with escape function
   document.write(unescape("Geeks%20for%20Geeks%21%21%21"));
     
   document.write("<br>");
     
   // Print encoded string using escape() function
   // Also include exceptions i.e. @ and .
   document.write(unescape("To%20contribute%20articles%20contact"+
                   "%20us%20atcontribute@geeksforgeeks.org"));
</script>
                     

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




<script>
   // Special character encoded with escape function
   var str = escape("Geeks for Geeks!!!");
   document.write("Encoded : " + str);
     
   // New Line
   document.write("<br>");
     
   // unescape() function
   document.write("Decoded : " + unescape(str))
     
   // New Line
   document.write("<br><br>");
     
   // The exception
   // @ and . not encoded.
   str = escape("To contribute articles contact us" +
                   "at contribute@geeksforgeeks.org")
   document.write("Encoded : " + str);
     
   // New Line
   document.write("<br>");
     
   // unescape() function
   document.write("Decoded : " + unescape(str))
</script>

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()

1.

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.

2.

A decoded string is returned.

An encoded string is returned.

3.

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: @ – + . / * _




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!