Explain the unescape () and string () functions in JavaScript _javascript tips

Source: Internet
Author: User

The Unescape () and string () functions in JavaScript are detailed in the following sections:

Definitions and usage

The JavaScript unescape () function decodes a string encoded by escape ().

Grammar

Unescape (String)

Parameters Description
String Necessary. The string to decode or reverse.

return value

A copy of a string that is decoded.

Description

This function works by finding a sequence of characters in the form%xx and%uxxxx (x represents a hexadecimal number), which is decoded by replacing such sequences with Unicode characters \u00xx and \uxxxx.

Tips and comments

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

Instance

In this case, we'll use escape () to encode the string and then decode it using unescape ():

<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 you to the JavaScript String () function

Definitions and usage

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

Grammar

String (object)

Parameters Description
Object Necessary. A JavaScript object.

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 00:17:40 utc+0800 2009
999 888
12345

These are the Unescape () and string () functions of the JavaScript described in the small series, I hope you like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.