Home »
JavaScript
escape() function with example in JavaScript
JavaScript escape() function: Here, we are going to learn about the escape() function with example in JavaScript, how and when it is used?
Submitted by IncludeHelp, on February 01, 2019
JavaScript escape() function
While transferring the data over the network or sometimes while saving data to the database, we need to encode the data. The function escape() is a predefined function in JavaScript, which encodes the given string.
It encodes almost all special characters including spaces.
Note: escape() function does not encode some of the special characters like @+-/.*_
Example
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
str = "Hello world!";
//Actual string
document.write("Actual string is: " + str);
document.write("<br>");
//encoded string
document.write("Encoded string is: " + escape(str));
document.write("<br>");
//assign another string
str = "email id: [email protected]";
//Actual string
document.write("Actual string is: " + str);
document.write("<br>");
//encoded string
document.write("Encoded string is: " + escape(str));
</script>
</body>
</html>
Output
Actual string is: Hello world!
Encoded string is: Hello%20world%21
Actual string is: email id: [email protected]
Encoded string is: email%20id%3A%[email protected]
JavaScript Built-in Functions »