JavaScript escape() Function
Last Updated :
13 Jan, 2023
The Javascript escape() function takes a string as a parameter and encodes it so that it can be transmitted to any computer in any network which supports ASCII characters.
Note:
escape(): This function was used to encode special characters in a string, but it has been replaced by the encodeURI() and encodeURIComponent() functions, which provide more options and better support for different character encodings.
Syntax:
escape(string)
Parameters: This function accepts a single parameter as mentioned above and described below:
- string: This parameter holds the string that will be encoded.
Return value: This function returns an encoded string.
Note: This function only encodes the special characters, this function is deprecated.
Exceptions: @ - + . / * _
Below is an example of the escape() function.
Example: In this example, we will simply encode a string with some signs using the JavaScript escape() Function.
javascript
<script>
// 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 [email protected]"));
</script>
Output:
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org
More example codes for the above function are as follows:
Example 1: In this example, we will simply encode a string with some signs using the JavaScript escape() Function.
javascript
<script>
// 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("A Computer Science Portal"));
</script>
Output:
Geeks%20for%20Geeks%21%21%21
A%20Computer%20Science%20Portal
Example 2: In this example, we will simply encode a string with some signs using the JavaScript escape() Function.
javascript
<script>
// Special character encoded with
// escape function
console.log(escape("GeeksforGeeks"));
// Print encoded string using escape() function
// Also include exceptions i.e. @ and .
console.log(escape("A#Computer-Science"+
"%Portal@for*Geeks"));
</script>
Output:
GeeksforGeeks
A%23Computer-Science%25Portal@for*Geeks
We have a complete list of Javascript Functions, to check those please go through Javascript Function Complete Reference article
Supported Browsers:
- Google Chrome 1 and above
- Internet Explorer 3 and above
- Edge 12 and above
- Mozilla Firefox 1 and above
- Safari 1 and above
- Opera 3 and above
Similar Reads
Underscore.js _.escape() function Underscore.js _.escape() function is used to escape a special character string from inserting into HTML. Some of the strings that get escape are "&", ">", "<", "''", etc. Note: Some special files are needed to be included while using this code directly in the browser. It is very necessary
1 min read
Less.js String escape() Function Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is preferable since CSS is a dynamic style sheet language. LESS can be utilized by many different browsers because it is versatile. Web browsers can only use CS
3 min read
JavaScript unescape() Function The Javascript unescape() function in JavaScript takes a string as a 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(). Note: The unescape() function is depreca
2 min read
JavaScript - Escape a String These are the following ways to Escape a String in JavaScript:1. Using BackslashesThe most straightforward way to escape characters is by using backslashes (\). This method allows you to include special characters like quotes (" or '), backslashes, and control characters within a string.JavaScriptle
1 min read
Difference between unescape() and escape() functions in JavaScript 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()
4 min read