JavaScript

What are escape and unescape String Functions in JavaScript

In JavaScript, escape() and unescape() methods are used to encode and decode strings. The JavaScript method unescape() can be used to restore the original string after using the escape() function to make a string accessible for transmission over a network.

This article will describe the escape and unescape functions in JavaScript.

What are escape String Functions in JavaScript?

The “escape()” function encodes the string to make it accessible for network transmission. It encodes all the non-ASCII characters to their two or four-digit hexadecimal numbers. Moreover, a blank space is converted to “%20” by the escape function.

Syntax

Follow the given syntax to use the escape() method:

escape(string)

Here, “string” is the parameter passed in a method for encoding.

Example

First, create a string named “str”:

var str = "(Welcome to Linuxhint!)";

Print the string on the page using the “document.write()” method:

document.write("String: " + str + "<br />");

Call the escape() method by passing a string as an argument and store the resultant encoded string in a variable “encodeStr”:

var encodeStr = escape(str);

Finally, print the encoded string on the page:

document.write("Encoded String: " + encodeStr + "<br />");

The output shows the original and encoded strings. Here, you can see that the “spaces” converted to “%20”, “(“ to “%28”, “!” to “%21” and “)” to “%29”:

What are unescape String Functions in JavaScript?

The unescape() method decodes the string in its original state. It converts all the hexadecimal values to the relevant character that they represent. Some browsers do not support this method, so for that, you can utilize the “ decodeURIComponent()” or “decodeURI()” as a replacement.

Syntax

Use the below-mentioned syntax for decoding the encoded string using unescape() method:

unescape(encodedString)

It takes an encoded string as a parameter.

Example

Here, call the unescape() method by passing an encoded string “encodeStr” as an argument:

var decodeStr = unescape(encodeStr);

Print the decoded string on the page:

document.write("Decoded String: " + decodeStr);

The output displays original, encoded, and decoded strings:

That’s all about escape and unescape functions in JavaScript.

Conclusion

The escape and unescape functions are used for encoding and decoding the strings to make a string accessible for transmission over a network. The escape() method converts all the non-ASCII values to their hexadecimal numbers, and the unescape() method will convert the encoded string into its original string by converting hexadecimal values to their non-ASCII characters. In this article, we described the escape and unescape functions in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.