Home >Web Front-end >JS Tutorial >Detailed explanation of Unescape() and String() functions in JavaScript_javascript skills

Detailed explanation of Unescape() and String() functions in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:33:141451browse

Detailed explanation of Unescape() and String() functions in JavaScript, the specific content is as follows:

Definition and usage

The JavaScript unescape() function can decode strings encoded by escape().

Grammar

unescape(string)

参数 描述
string 必需。要解码或反转义的字符串。

Return value

A decoded copy of string.

Description

This function works by decoding by finding character sequences of the form %xx and %uxxxx (x represents a hexadecimal number) and replacing such character sequences with the Unicode characters u00xx and uxxxx.

Tips and Notes

Note: ECMAScript v3 has removed the unescape() function from the standard and deprecated its use, so it should be replaced by decodeURI() and decodeURIComponent().

Example

In this example we will use escape() to encode the string and then unescape() to decode it:

<script type="text/javascript">
var test1="Visit W3School!"
test1=escape(test1)
document.write (test1 + "<br />")
test1=unescape(test1)
document.write(test1 + "<br />")
</script>

Output:

Visit W3School!
Visit W3School!
TIY
unescape()

Let me introduce to you the JavaScript String() function

Definition and usage

String() function converts the value of the object into a string.

Grammar

String(object)

参数 描述
object 必需。JavaScript 对象。

Example

In this example we will try to convert different objects into strings:

<script type="text/javascript">
var test1= new Boolean(1);
var test2= new Boolean(0);
var test3= new Boolean(true);
var test4= new Boolean(false);
var test5= new Date();
var test6= new String("999 888");
var test7=12345;
document.write(String(test1)+ "<br />");
document.write(String(test2)+ "<br />");
document.write(String(test3)+ "<br />");
document.write(String(test4)+ "<br />");
document.write(String(test5)+ "<br />");
document.write(String(test6)+ "<br />");
document.write(String(test7)+ "<br />");
</script>

Output:

true
false
true
false
Wed Oct 28 00:17:40 UTC 0800 2009
999 888
12345

The above is the unescape() and String() functions in JavaScript introduced by the editor. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn