Data type conversions in JavaScript

Source: Internet
Author: User

Convert to Number:

parseint (); Convert to integer value, starting from subscript 0, if the numeric type continues until a non-numeric value is encountered, the preceding integer values are returned;
The decimal point is not valid and returns Nan if 0 is non-numeric;
Converting an empty string returns Nan;

Can be converted to a different binary, such as Var a=parseint ("123", 2);//return result is 2 binary

Parsefloat (); Convert to decimal, starting with subscript 0, only 10 binary form; If there is a second decimal point, the second decimal point is invalid and all is discarded.
Returns the front face value, which returns Nan if 0 starts with a non-numeric value, ignores the leading 0, such as: Var b=parsefloat (010);//10;

To force type conversions:

Boolean (); Any non-empty string is true; any non-0 numeric value (including infinity) is true; Any object is true, NULL is false;undefined can only be false;

Number (); If Boolean, True and false convert to 1/0;null convert to 0;undefined to Nan;

String: Contains only numbers (including positive, negative sign) converted to 10 decimal, leading 0 ignored; if floating-point numbers are converted to floating-point values, leading 0 is ignored;

Contains 16 binary, converted to a decimal value of the same size, or 0 if NULL, if the conversion to Nan is included in the preceding several;

Object: Invokes the ValueOf () method of the object and then transforms the return value according to the preceding rule, or ToString () If the result is Nan;

The preceding rule transformation returns a string value.

Number (); parseint (); The difference is that if there is a non-number behind the preceding number in the string, the former returns Nan, and the latter returns numbers that are not in front of the number;

Handles an empty string, which returns 0, while the latter returns Nan;

String (), numeric, Boolean, String value, object has the Tosting () method, null, undefined not, so you cannot use the same ToString () method to convert this

2 kinds, otherwise it will error, this is the difference between the string () and ToString ();

Conversion rules:

If the value has the ToString () method, the method is called to return the corresponding result; "NULL" is returned if null, and "undefined" is returned for undefined;

Data type conversions in JavaScript

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.