Detail the Unescape of and String of functions in JavaScript

  • 2020-10-23 20:50:57
  • OfStack

The Unescape() and String() functions in JavaScript are explained in detail as follows:

Definition and usage

The JavaScript unescape() function decodes strings encoded through escape().

grammar

unescape(string)

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

The return value

1 copy of string decoded.

instructions

The function works like this: By finding character sequences in the form %xx and %uxxxx (x stands for digits in decimal 106), replace such character sequences with Unicode characters \u00xx and \uxxxx to decode.

Hints and comments

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

The instance

In this example, we will use escape() to encode the string and then use 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%20W3School%21
Visit W3School!
TIY
unescape()

Let me introduce the JavaScript String() function

Definition and usage

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

grammar

String(object)

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

The instance

In this example, we will try to convert different objects to 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

Above is the unescape() and String() functions in JavaScript introduced by this site, I hope you like them.


Related articles: