The interaction between JavaScript three data types

Source: Internet
Author: User

One: number<===>string conversions between numeric types and string types

There are three ways to number===>string a number to a string:

1. After the number + "";

2. The wrapper type string (number).

3. The toString () method in the prototype prototype of type object;

    var num=10;     var str1=num+ "";     var str2=String (num);     var str3=num.tostring ();     typeof (str1) + "<br/>");     typeof (str2) + "<br/>");     typeof

Output:

10string10string10string

Here are a few of the few things you need to know: toFixed (number of decimal places);

Toexponential ()//index indication;

Toprecision ()//number of significant digits;

String===>number String type to numeric type

1. Multiply the string by 1;

2. Reduce the string by 0;

3. The type of packaging using the numeric type number (str);

4.parseInt (str)/parsefloat (str);

    varStr= "10"; varN1=str*1; varn2=str-0; varn3=Number (str); varn4=parseint (str); varn5=parsefloat (str); document.write (N1+ " " +typeof(N1) + "<br/>"); document.write (N2+ " " +typeof(n2) + "<br/>"); document.write (N3+ " " +typeof(n3) + "<br/>"); document.write (N4+ " " +typeof(N4) + "<br/>"); document.write (N5+ " " +typeof(N5) + "<br/>");

Results:

Number Ten number number ten number 

Two: Boolean<===>number,string Boolean type and number type, mutual transfer between string types

Number===>boolean conversion of numeric types to Boolean types

1. Non-zero is true, 0 is false----implicit conversion

2.Boolean (number)----Display conversion

3.!! Number--Take the inverse two times

String===>boolean String type conversion to Boolean type

1. Non-empty strings are true, empty strings are false (spaces are not empty strings)----implicit conversions

2.Boolean (String)----display Conversion

3.!! String--Take the inverse two times

Boolean===>number Boolean type to numeric type

Number (Boolean) true corresponds to 1; False corresponds to 0.

Boolean===>string Boolean type to String type

String (Boolean) true corresponds to true; False corresponds to False.

Any non-empty objects (object) are true. Null,undefined is true.

Three: | | and && has another purpose besides doing logical operations

* In | | If the object is placed on both sides, first determine whether the left object is true, if the left object is returned, otherwise the right object is returned, if the right object is also true, returns undefined.

* On both sides of && If the object is placed, first determine whether the left object is false, if the left object is returned, otherwise the right object, if the right object is also false, return undefied.

The interaction between JavaScript three data types

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.